How to maintain fragment\'s state when it is shown within FragmentTabHost?
Thanks to this tutorial, I\'m able to implement FragmentTabHost in my applica
I believe your fragment is being re-instantiated each time you switch tab, which means that your field variables are reset.
You probably could use the saveInstance bundle to manage the state of your fragment but I find it more useful and simpler to use SharedPreferences. This also has the benefit of keeping the saved state even if your application is restarted.
To read and write variables to SharedPreferences I use this small helper class:
public class PreferencesData {
public static void saveString(Context context, String key, String value) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putString(key, value).commit();
}
public static void saveInt(Context context, String key, int value) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putInt(key, value).commit();
}
public static void saveBoolean(Context context, String key, boolean value) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putBoolean(key, value).commit();
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPrefs.getInt(key, defaultValue);
}
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPrefs.getString(key, defaultValue);
}
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPrefs.getBoolean(key, defaultValue);
}
}
Now, as an example, to save your mIsViewInitiated variable, then in onPause:
@Override
protected void onPause() {
PreferencesData.saveBoolean(this, "isViewInitiated", mIsViewInitiated);
super.onPause();
}
And to retrieve it again:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
try {
super.onActivityCreated(savedInstanceState);
Log.e("AudioContainerFragmentClass", "onActivityCreated called");
// will now be true if onPause have been called
mIsViewInitiated = PreferencesData.getBoolean(this, "isViewInitiated", false);
if (!mIsViewInitiated) {
mIsViewInitiated = true;
initView();
}
} catch (Exception e) {
printException(e.toString());
}
}
Since this example variable tells whether some UI has been loaded, then you might want to set it to false when the activity is destroyed.
@Override
protected void onDestroy() {
PreferencesData.saveBoolean(this, "isViewInitiated", false);
super.onDestroy();
}
This answer is just a single option and shows my personal preference, whereas other options might suit your situation better. I would suggest taking a look at http://developer.android.com/guide/topics/data/data-storage.html