Best Practice for Nested Fragments in Android 4.0, 4.1 (<4.2) Without Using the Support Library

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 Fragments 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 Fragments: 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:

  1. Use a standard TabHost and implement your own logic to switch the fragments when the tab changes
  2. 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 the FragmentTabHost 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



Leave a reply



Submit