Fragment within ViewPager does not call any method

0

I have an activity ( MainActivity ) that contains a PagerFragment that has a ViewPager . In the pager I have 4 different tabs.

Within each tab I have a Fragment 1 that can be replaced using FragmentManager.replace() and the addition to backStack ( addToBackStack() )

Everything works normally, but I noticed a problem that I could not find anywhere:

When this Fragment 1 , within ViewPager , calls replace(new Fragment2() ).addToBackStack(null) , my fragment is replaced correctly.

However, when I hit the back button, Fragment2 is destroyed correctly, but the Fragment1 that was in backstack does not call its onCreateView , or onResume .

Another thing I noticed is that when I call replace , onDestroy of Fragment1 is not called.

What's wrong?

EDIT

Talking to @Wakim, we came to the conclusion that I am replacing the Fragments in the wrong way: In% with% of% with%: onCreateView

And in%% of a button responsible for replace:

FragmentManager fragmentManager = mContext.getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .replace(R.id.view_lists_fragment, new CreateListFragment())
                        .addToBackStack(null)
                        .commit();

Fragment1 is the layout container of my mView = inflater.inflate(R.layout.fragment_lists, container, false);

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:id="@+id/view_lists_fragment"
tools:context="com.kayan.letsapp.Fragments.ListsFragment"> 
.... 
</RelativeLayout>
    
asked by anonymous 19.04.2015 / 18:41

1 answer

1

Following the source code of the project, what I understood that has to be done is the following:

1 - Creation of the main Fragment

FragmentStatePagerAdapter calls getItem , so you should return the Fragment main of that position, this guy I call RootFragment (I believe that in your case it would be Fragment1 , but I'll use another nomenclature, following the project you mentioned).

The RootFragment adds the Fragment1 that is initially visible. Fragment1 is the starting child.

I think it would be even better to use getChildFragmentManager , but I do not know if it's necessary.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.root_fragment, container, false);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.root_frame, new Fragment1());
    transaction.commit();

    return view;
}

2 - Replace of Fragment1 with CreateListFragment .

It would delegate to RootFragment this responsibility:

public void replaceFragment() {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.replace(R.id.root_frame, new CreateListFragment());
    transaction.addToBackStack(null);

    transaction.commit();
}

At the end you would have something like:

  

FragmentStatePagerAdapter - > add RootFragment

     

RootFragment - > add Fragment1

     

RootFragment - > replace Fragment1 to CreateListFragment

3 - Removing CreateListFragment

When you do FragmentManager.popBackStack() the CreateListFragment will be destroyed ( onDestroView and onDestroy will be called).

And Fragment1 will be restored ( onResume and onCreateView will be called).

Remembering that I'm not so sure how this works because I've never had to implement something like that. But I think it should work

    
19.04.2015 / 19:43