This is not a question of how I can avoid my activity restart and recreation on say config changes lie e.t.c This is a question of why, despite all my efforts, my activity still restarts.What could be the possible reason?
Here is my scenario.
My app a text editing application, with tabs, so I implemented a custom TabHost and a custom TabWidget. The app has the MainActivity and a single fragment, MainFragment.
On To ensure instance is retained in my MainFragment
, I have these:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
To ensure MainActivity
instance is retained, I have this:
/**
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(android.R.id.content);
setContentView(frame, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
actionBar = getSupportActionBar();
if ((MyTextEditor.isSmallScreenSize(getResources()) || MyTextEditor.isNormalScreenSize(getResources()))
&& getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ) {
actionBar.setDisplayShowTitleEnabled(false);
}
if (savedInstanceState != null) {
editorFrag = (MyTedFragment) getSupportFragmentManager().getFragment(
savedInstanceState, "editor");
}
if (editorFrag == null) {
editorFrag = new MyTedFragment();
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, editorFrag).commit();
}
....
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "editor", editorFrag);
}
And the following in my AndroidManifest.xml
<application
android:name="com.myapp.MyApp"
android:debuggable="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyAppTheme" >
<!-- Ted main activity ... ie a text field :)
android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="adjustResize"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|fontScale|screenSize|smallestScreenSize">
-->
<activity
android:name="com.myapp.texteditor.MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|fontScale|screenSize|smallestScreenSize"
android:launchMode="singleTask" android:windowSoftInputMode="adjustResize">
</activity>
....
</application>
Now the problem:
CASE 1: Android version: 2.3.6 Gingerbread on Samsung device
If I push my activity to the background and I relaunch my app in less than about 3 minutes , the main activity instance is restored just fine. If I choose to relaunch my app after a while longer, say 10 min or so,the app restarts afresh and this is bad for my app because if a user had any unsaved changes, they are lost.
CASE 2: Android Version: 4.3, running on Emulator
How long I choose to wait before relaunching the app, the instance is always restored. Looks like everything works fine.
What could be the reason for this? Is this associated with a bug around the activity life cycle or Activity Manager for Gingerbread?