Android 4.2: back stack behaviour with nested fragments

This solution may be better version of @Sean answer: @Override public void onBackPressed() { // if there is a fragment and the back stack of this fragment is not empty, // then emulate ‘onBackPressed’ behaviour, because in default, it is not working FragmentManager fm = getSupportFragmentManager(); for (Fragment frag : fm.getFragments()) { if (frag.isVisible()) { … Read more

Nested fragments disappear during transition animation

So there seem to be a lot of different workarounds for this, but based on @Jayd16’s answer, I think I’ve found a pretty solid catch-all solution that still allows for custom transition animations on child fragments, and doesn’t require doing a bitmap cache of the layout. Have a BaseFragment class that extends Fragment, and make … Read more

How to set a ViewPager inside a Fragment

Starting in Android 4.2, there are nested fragments.http://developer.android.com/about/versions/android-4.2.html#NestedFragments The support library now also includes support for this for older Android versions. So you can do something like this: @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager); mViewPager.setAdapter(new MyAdapter(getChildFragmentManager())); } Full implementation available here: https://github.com/marcoRS/nested-fragments/tree/master/src/com/burnside/digital/nestedfragments

Sending data from nested fragments to parent fragment

The best way is use an interface: Declare an interface in the nest fragment // Container Activity or Fragment must implement this interface public interface OnPlayerSelectionSetListener { public void onPlayerSelectionSet(List<Player> players_ist); } Attach the interface to parent fragment // In the child fragment. public void onAttachToParentFragment(Fragment fragment) { try { mOnPlayerSelectionSetListener = (OnPlayerSelectionSetListener)fragment; } catch … Read more

onActivityResult() not called in new nested fragment API

I solved this problem with the following code (support library is used): In container fragment override onActivityResult in this way: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); List<Fragment> fragments = getChildFragmentManager().getFragments(); if (fragments != null) { for (Fragment fragment : fragments) { fragment.onActivityResult(requestCode, resultCode, data); } } } Now … Read more

Best practice for nested fragments in Android 4.0, 4.1 (

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 … Read more