I have an activity ( MainActivity
) that contains a PagerFragment
that has a ViewPager
. In the pager I have 4 different tabs.
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>