Nested Fragments and The Back Stack
I have the same problem, I would like to nest fragments, and to keep a back stack for each nested fragment.
But... it seems that this case is not handled by the v4 support library. In the FragmentActivity code in the library, I can find :
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
The mFragments represents the FragmentManager of the activity, but it does not seem this manager "propagates" the pop to children managers.
A workaround would be to manually call the popBackStackImmediate() on the child manager, like this in the activity inherited from FragmentActivity :
private Fragment myFragmentContainer;
@Override
public void onBackPressed() {
if (!myFragmentContainer.getChildFragmentManager().popBackStackImmediate()) {
finish(); //or call the popBackStack on the container if necessary
}
}
There might be a better way, and a more automated way, but for my needs it is allright.
Best practice for nested fragments in Android 4.0, 4.1 (4.2) without using the support library
Limitations
So nesting fragments inside another fragment is not possible with xml regardless of which version of FragmentManager
you use.
So you have to add fragments via code, this might seem like a problem, but in the long run makes your layouts superflexible.
So nesting without using getChildFragmentManger
? The essence behind childFragmentManager
is that it defers loading until the previous fragment transaction has finished. And of course it was only naturally supported in 4.2 or the support library.
Nesting without ChildManager - Solution
Solution, Sure! I have been doing this for a long time now, (since the ViewPager
was announced).
See below; This is a Fragment
that defers loading, so Fragment
s can be loaded inside of it.
Its pretty simple, the Handler
is a really really handy class, effectively the handler waits for a space to execute on the main thread after the current fragment transaction has finished committing (as fragments interfere with the UI they run on the main thread).
// Remember this is an example, you will need to modify to work with your code
private final Handler handler = new Handler();
private Runnable runPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.frag_layout, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
runPager = new Runnable() {
@Override
public void run()
{
getFragmentManager().beginTransaction().addFragment(R.id.frag_container, MyFragment.newInstance()).commit();
}
};
handler.post(runPager);
}
/**
* @see android.support.v4.app.Fragment#onPause()
*/
@Override
public void onPause()
{
super.onPause();
handler.removeCallbacks(runPager);
}
I wouldn't consider it 'best practice', but I have live apps using this hack and I am yet to have any issues with it.
I also use this method for embedding view pagers - https://gist.github.com/chrisjenx/3405429
go back button between grand child fragment
This is probably the bug mentioned here: https://code.google.com/p/android/issues/detail?id=40323
You can workaround it by handling the 'back' manually. Refer to this thread for a lot of workarounds: Android 4.2: back stack behaviour with nested fragments
Related Topics
Android: Is Using Setcontentview Multiple Times Bad While Changing Layouts
Problem About SQLite Database, No Such Table
Handling Keepsynced() While on Background on Android and with Fcm
How to Get Meid and Imei Information Using Adb Commands on Android 5.0
My App Was Rejected by Instagram Basic Display API Review Due to Invalid Reasons
Best Way to Save Large Amount of Data Locally in Unity3D Android
Which Port and Protocol Does Google Cloud Messaging (Gcm) Use
Firebase Messaging Nosuchmethoderror.Zzur Exception
Android Facebook Authorization - Can Not Log in When Official Facebook App Is Installed
Canonical Registration Id and Message Id Format
Android Usb Host API and Usb Storage
Sending Intent with Bundle Using Console
Android Httpclient:Networkonmainthreadexception
Problem Loading Swf File in Android
Batch Scripting - If Exist ./Sdcard/File.Any Using Adb
How to Fill Recyclerview with Gridlayoutmanager from Right to Left
How to Display Fetched JSON Data into Listview Using Baseadapter