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
Nested fragments good practice?
The "container" Fragment
is not a good idea, simply because it will mean two layers of nested Fragment
s: the outermost "container" Fragment
, inside it the Fragment
which holds the ViewPager
, and the ViewPager
itself which holds a set of Fragments
.
It would be better to not have the "container" Fragment
at all. Keep the rest same.
something similar to FragmentTabHost without support(v4) library
As you have a minimum SDK of 14
(and I think you're not using a ViewPager
?!) it would make sense to use the SDK version of the fragments framework. A FragmentTabHost
is a convenience wrapper around a TabHost
to make working with fragments as tabs easier. I don't recall seeing an equivalent for FragmentTabHost
in the SDK so you would have two options:
- Use a standard
TabHost
and implement your own logic to switch the fragments when the tab changes - Copy the code for the
FragmentTabHost
and change it to work with the SDK version of the fragments framework. If I remember right the code for theFragmentTabHost
is fairly simple and a copy-paste followed by changing the imports should work.
However, it seems you want to use the FragmentTabHost
with nested fragments(the fragment tabs being part of another bigger fragment), in this case you need to use the fragments from the support library(along with FragmentTabHost
) because the support for nested fragments was introduced in the SDK starting with 4.2
which means that any version between 4.0 and 4.2 will not be able to use nested fragments.
share argument between all fragments
- You can use shareviewmodel, it's best practice
- Link refer: viewmodel
Related Topics
Androidx:Appcompat I:Art Error Android.View.View$Onunhandledkeyeventlistener
How to Debug Android Application Line by Line Using Eclipse
How to Make Grid-View Horizontally Scrollable in Android
Can't Run a Java Android Program with Valgrind
How to Detect Device Is Android Phone or Android Tablet
Save Image to Sdcard from Drawable Resource on Android
Coupling Firebaserecyclerviewadapter to a Boolean/String Map.Entry
How to Set Custom Font for a Whole Application in Android
How to Disable Keypad Popup When on Edittext
How to Store Images Using Sharedpreference in Android
How to Detect When a User Plugs Headset on Android Device? (Opposite of Action_Audio_Becoming_Noisy)
Android Studio 3.1 "Run" Not Compiling Code
Service Not Available - Geocoder Android
Device Not Detected in Eclipse When Connected with Usb Cable