Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager blank on back from another fragment

Actually, I am using whole application in a single activity by fragments in main view container. One fragment contains a viewPager when I clicked over a header button fragment replaced by LoginFragment and on Back we maintaining backstack so fragment containing viewPager will be on top but there we got blank view of current item in viewPager.

like image 266
Pradeep Kumar Avatar asked Jul 30 '14 17:07

Pradeep Kumar


2 Answers

This is my Solution and i have searched a lot of the answer so if you got a better one please share it with me..

i switched the ViewPager to the Activity

Activity XML

<RelativeLayout ....
   xmlns.

<FrameLayout 
    android:id="@+id/container"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
 />

 <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 </...viewPager>

Activity Class

public MainActivity extends Activity .... {

   private boolean mShowPager;
   private FrameLayout mContainer;
   private ViewPager mPager;

   //Inflate them


  public void showViewPager() {
        mPager.setVisibility(View.Visible);
        mContainer.setVisibilty(View.Gone);
  }

  public void showContainer() {
        mContainer.setVisibility(View.Visible);
        mPager.setVisibilty(View.Gone);
  }


  public void showViewPager(boolean show) {
       mShowPager = show;
  }


  @override
  public void onBackPressed() {

        if(mShowPager) {
             showViewPager();
        }
        super.onBackPressed();

Fragment Example

  public void onAttach(Activity activity) {

         ((MainActivity)activity).showViewPager(true);
         ((MainActivity)activity).showContainer();

//True means when back pressed back show ViewPager //Back means still show Container but do Back pressed Info : you decide what you want to show ViewPager or the Container that contain the Fragments plus you can use the BackStack if needed.

Hope it helps its helped me.

like image 87
Itzik Samara Avatar answered Sep 28 '22 01:09

Itzik Samara


I got an answer by my self on debugging. Problem was I was using fragmentPager inside fragment so there for adapter initialization we have to pass getChildFragmentManager() instead of getFragmentManager()

like image 32
Pradeep Kumar Avatar answered Sep 28 '22 01:09

Pradeep Kumar