Skip to content Skip to sidebar Skip to footer

How To Correctly Using Fragments

In my base Activity class I have links to all fragments that I call from menu. Something like this: BaseFragment mProjects; BaseFragment mProfile; public BaseFragment getFragment(

Solution 1:

Maybe you make a mistake in your FragmentActivity.

Perhaps you override onSaveInstanceState without call super.onSaveInstanceState(Bundle outState) in your FragmentActivity

This will cause the method ** onSaveInstanceState** not called in fragment

UPDATE Alternatively you could use hide/show instead of replace when you want to show new fragments... the hided fragment is not killed

Solution 2:

Before you replace a currently shown fragment, you need to getSupportFragmentManager().putFragment(Bundle, String, Fragment), this will store that fragment into a Bundle which you can keep a reference to, and save to your activity's instance state.

In your getFragment method, first check to see if the bundle you have been saving your fragments into contains the fragment you want to create - getSupportFragmentManager().getFragment(Bundle, String). This will give you a fragment you can return and replace. If not, create a new instance of the fragment as you currently are.

Post a Comment for "How To Correctly Using Fragments"