I've got a tabbed application using Fragment
s, and a slight problem.
MainActivity
(extends FragmentActivity
- with no code save for the layout specification on onCreate
) includes a Fragment
called TabsFragment
(extends Fragment
, implements OnTabChangeListener
) in its layout (which houses the tabs themselves in a FrameLayout
), switching out sub-Fragment
s using Transaction
s.
The latter sub-Fragments
are the ones with the actual content whose state I wish to save when the user turns the device, visits another application, answers a phone call, or does something else. Currently the state of the sub-Fragment
s are not saved, but the activity remembers which sub-Fragment
is being shown. (So if I have the device in landscape mode and turn the device while tab 3 is active, tab 3 will appear in portrait mode, reset, but showing. So some state is being preserved without me having done anything).
Because I have multiple layouts (landscape and portrait), android:configChanges="orientation"
in the manifest is not an option - I wouldn't want to use it anyway given that it's a glorified bandaid.
I'm using the v4 support library (rev 10) and after scouring the API, I came across FragmentManager.saveFragmentInstanceState()
and Fragment.setInitialSavedState(Fragment f)
. I have put the saveFragmentInstanceState() for each sub-Fragment
in the overridden onPause()
method of TabsFragment
. I'm not sure if this works, because wherever I put the setInitialSavedState
, it gives an IllegalStateException
- but this happens even when I have just initialised a new sub-Fragment
tab. It also crashes when I put it into onResume
in TabsFragment
.
Code snippet:
//... this is the 'showTab' method
if(getFragmentManager().findFragmentByTag(id) == null)
{
Fragment f = null;
if(BASIC_TAB.equals(id))
{
f = new BasicTabFragment();
f.setInitialSavedState(basicState); // basicState was set in onPause()
getFragmentManager().beginTransaction().replace(tabNo, f, tabID).commit();
}
}
If similar code is placed in onResume
, I get the following error:
Error snippet:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity (org.example.App/org.example.App/MainActivity): java.lang.IllegalStateException: Fragment already active
-snip-
Caused by: java.lang.IllegalStateException: Fragment already active at android.support.v4.app.Fragment.setIniailSavedState(Fragment.java:507) at org.example.App.TabsFragment.onResume(TabsFragment.java:223)
etc.
Clearly I'm calling setInitialSavedState at the wrong point in the lifecycle, but it's not clear to me when this should happen.
Also I'm beginning to wonder if FragmentManager.saveFragmentInstanceState() is indeed the best thing to use here given the somewhat complex nature of the tabbed setup. If so, what to use? How can I save the state of my application's tabs?
onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.
A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.
Fragment is added and its layout is inflated. Fragment is active and visible.
When an Activity is destroyed, the Fragments are detached from the Activity and re-attached when it is recreated. Therefore, the fragments should still exist in their current state. When re-attached, OnCreateView
is called again. If you're initializing everything as if it's from scratch (ie. setting lists to new ArrayList()
) you will be wiping out the state of the fragment.
Try making an instance variable within your Fragment for everything that needs to be preserved (ie. a String for any text fields - EditText fields will be automatically saved as you mention). Example:
String text; TextView tv; public View onCreateView(LayoutInflater i, ViewGroup vg, Bundle b) { // this will re-inflate everything from the layout. If you initialize any text fields here, it will reset them to the value you set in the xml file View v = i.inflate(R.layout.myLayout, vg, false); if(text != null) tv.setText(text); }
When your TextView value changes (say in an OnClickListener or similar), set the value of text
in addition to whatever else you do with it. When the fragment is re-attached, it will then set it to the last change that was made.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With