title: ViewPager get invisible after coming from navigation drawer
body: <p>I have been developing an app which contain navigation drawer on main page and on main page I have an framelayout in which I am displaying HomeFragment.java which contain viewPager.
  The problem is when I launch my application the HomeFragment.java gets called and viewpager works fine. But when I click Home but in navigation drawer which call HomeFragment.java then the view pager gets invisible. Here are my code.
  Please Help.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.motomojoapp.R;
  import com.motomojoapp.fragment.FragmentDrawer;
  
  public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
  
  Toolbar toolbar;
  TextView textView;
  private FragmentDrawer drawerFragment;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      toolbar = (Toolbar) findViewById(R.id.toolbar_white);
      setToolbar();
      drawerFragment = (FragmentDrawer)
              getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
      drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
      drawerFragment.setDrawerListener(this);
      FragmentManager fragmentManager = this.getSupportFragmentManager();
      Fragment fragment = new HomeFragment(fragmentManager);
      if (fragment != null) {
  
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          fragmentTransaction.replace(R.id.container_body, fragment);
          fragmentTransaction.commit();
  
          // set the toolbar title
          getSupportActionBar().setTitle("");
      }
  }
  
  public void setToolbar() {
      toolbar.setTitle("");
      textView = (TextView) findViewById(R.id.toolbar_title_white);
      textView.setText(getResources().getString(R.string.home));
      toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimary));
      setSupportActionBar(toolbar);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.notification) {
          startActivity(new Intent(this, NotificationActivity.class));
          return true;
      }else if (id == R.id.map) {
          startActivity(new Intent(this, MapActivity.class));
          return true;
      }
  
      //noinspection SimplifiableIfStatement
  
  
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onDrawerItemSelected(View view, int position) {
      displayView(position);
  }
  private void displayView(int position) {
      switch (position) {
          case 0:
              FragmentManager fragmentManager = this.getSupportFragmentManager();
              Fragment fragment = new HomeFragment(fragmentManager);
              if (fragment != null) {
  
                  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                  fragmentTransaction.replace(R.id.container_body, fragment);
                  fragmentTransaction.commit();
  
                  // set the toolbar title
                  getSupportActionBar().setTitle("");
              }
  
              break;
          case 1:
          case 2:
              break;
          default:
              FragmentManager fragmentManager1 = this.getSupportFragmentManager();
              Fragment fragment1 = new HomeFragment(fragmentManager1);
              if (fragment1 != null) {
  
                  FragmentTransaction fragmentTransaction = fragmentManager1.beginTransaction();
                  fragmentTransaction.replace(R.id.container_body, fragment1);
                  fragmentTransaction.commit();
  
                  // set the toolbar title
                  getSupportActionBar().setTitle("");
              }
              break;
      }
  }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;LinearLayout
              android:id="@+id/container_toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
  
              &lt;include
                  android:id="@+id/toolbar_white"
                  layout="@layout/toolbar_white" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;FrameLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/container_body"
              android:layout_marginTop="6dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="1"&gt;
          &lt;/FrameLayout&gt;
      &lt;/LinearLayout&gt;
      &lt;fragment
          android:id="@+id/fragment_navigation_drawer"
          android:name="com.motomojoapp.fragment.FragmentDrawer"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:layout="@layout/fragment_navigation_drawer"
          tools:layout="@layout/fragment_navigation_drawer" /&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>HomeFragment.java</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.view.ViewPager;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.motomojoapp.R;
  import com.motomojoapp.customadapter.SlideAdapter;
  
  public class HomeFragment extends Fragment {
      private SlideAdapter adapter;
      private ViewPager viewPager;
      FragmentManager fragmentManager ;
  
      public HomeFragment(FragmentManager fragmentManager) {
          this.fragmentManager = fragmentManager;
      }
  
      public HomeFragment() {
          // Required empty public constructor11
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_home, container, false);
          viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
          viewPager.setClipToPadding(false);
          viewPager.setPageMargin(35);
          adapter = new SlideAdapter(fragmentManager);
          viewPager.setAdapter(adapter);
          viewPager.setCurrentItem(adapter.FIRST_PAGE);
  
          // Inflate the layout for this fragment
          return rootView;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  }
  </code></pre>
  
  <p>fragment_home.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
      &lt;android.support.v4.view.ViewPager
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/viewPager"
          android:layout_width="match_parent"
          android:layout_height="250dp"
          android:layout_marginTop="6dp"
          android:paddingLeft="34dp"
          android:paddingBottom="16dp"
          android:paddingTop="16dp"
          android:paddingRight="12dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>SlideAdapter.java</p>
  
  <pre><code>import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  
  import com.motomojoapp.fragment.SlideFragment;
  
      public class SlideAdapter extends FragmentPagerAdapter {
      private int PAGES = 3;
      // You can choose a bigger number for LOOPS, but you know, nobody will fling
      // more than 1000 times just in order to test your "infinite" ViewPager :D
      private int LOOPS = 1000;
      public int FIRST_PAGE = PAGES * LOOPS / 2;
  
      public SlideAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      @Override
      public Fragment getItem(int position) {
          position = position % PAGES;
          return SlideFragment.newInstance(Integer.toString(position + 1));
      }
  
      @Override
      public int getCount() {
          return PAGES * LOOPS;
      }
  
      @Override
      public float getPageWidth(int position) {
          return 0.93f;
      }
  
  }
  </code></pre>
  
  <p>SlideFragment.java</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.FrameLayout;
  import android.widget.TextView;
  
  import com.motomojoapp.R;
  
     public class SlideFragment extends Fragment {
      private static final String ARG_PAGE_NUMBER = "pageNumber";
      private String mParam;
      private TextView tvPos;
  
      public SlideFragment() {
          // Required empty public constructor
      }
  
      public static SlideFragment newInstance(String pageNumber) {
          Bundle args = new Bundle();
          args.putString(ARG_PAGE_NUMBER, pageNumber);
  
          SlideFragment fragment = new SlideFragment();
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (getArguments() != null) {
              mParam = getArguments().getString(ARG_PAGE_NUMBER);
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          FrameLayout root = (FrameLayout) inflater.inflate(R.layout.fragment_slide, container, false);
          tvPos = (TextView) root.findViewById(R.id.text);
          tvPos.setText("Page " + mParam);
          return root;
      }
  
  }
  </code></pre>