0
I have an activity (MainActivity) containing a PagerFragment who owns a ViewPager. On the pager I have 4 different tabs.
Within each tab I own one Fragment 1 which can be replaced by using the FragmentManager.replace() and add it to backStack (addToBackStack())
Everything works normally, but I noticed a problem that I couldn’t find anywhere:
When that Fragment 1 , within the ViewPager, calls the replace(new Fragment2() ).addToBackStack(null), my Fragment is replaced correctly.
However, when I press the back button, Fragment2 is destroyed correctly, but the Fragment1 who was in the backstack, doesn’t call his onCreateView, or onResume.
Another thing I noticed, is that when I call the replace, the onDestroy of Fragment1 is not called.
What is wrong?
EDIT
Talking to @Wakim, we came to the conclusion that I am replacing Fragments in the wrong way:
In the onCreateView of Fragment1:
mView = inflater.inflate(R.layout.fragment_lists, container, false);
And in the onClickListener 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();
The R.id.view_lists_fragment is the container of my layout Fragment1
<?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>
What do you call "father fragment"? The
Fragmentthat hosts theViewPageror theFragmentprevious within theViewPager?– Wakim
Edited question, @Wakim
– Kayan Almeida
Kayan, is using the
FragmentPagerAdapterto make that management or is adding theFragmentson its own account?– Wakim
I’m using the
FragmentStatePagerAdapterto return the 4 different fragments in tabs.replace... I’m taking a look at this project now: https://github.com/danilao/fragments-viewpager-example/blob/master/src/com/pineappslab/frcontainer/RootFragment.java And it looks like it creates a Rootfragment, I guess I’m not replacing the fragments properly inside the pager view– Kayan Almeida
Hmmm, I think the problem is in that
replace. The ideal is that it does not interfere with the managementFragmentStatePagerAdaptermakes, it may be that he is getting lost... What you want is to exchange a certainFragmentwithin one position for another and then be able to return?– Wakim
That, except when I come back, the Fragment I was before should call onResume, or onCreate, so I can update certain things. I need at least something to be called by
Fragment1!– Kayan Almeida
Can you see if the
onDestroyViewand theonCreateVieware being called? For me these two should be called because of thereplaceand ofbackStack.– Wakim
onDestroyViewis not being called, I’m definitely doing this replace wrong! I think I’m using theidwrong!– Kayan Almeida
@Wakim, I edited my question, if I could help an Eng. friend of ufrj computing, I would be eternally grateful ;D
– Kayan Almeida
I think that’s right, but only that you would need to add one
Fragmentwithin theonCreateViewofFragment1to be removed. I believe you’re doing it right (I’ll call it Rootfragment)? If so, in theonCreateViewofFragment1you would add theRootFragmentin theview_lists_fragmentand then would replace him byCreateListFragment. In that case when you get back, it’sonCreateViewandonResumeofRootFragmentthat will be called. TheFragment1has always been visible/active so its life cycle is not changed.– Wakim
@Wakim, I understood, The correct thing then is to have a dummy Fragment that contains Fragments, because this Fragment Dummy, being in Viewpager, is not destroyed, right?
– Kayan Almeida
That’s right. I took a look at the answer to see if I made it clear hehe
– Wakim