First, I have two activities: Splash and MainActivity ( only support portrait). In MainActivity, I have many fragments use Slide menu. I want to keep current fragment when user leave MainActivity. Here is my try:
int currentFragment = 0;
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
currentFragment = savedInstanceState.getInt(CURRENT_FRAGMENT_KEY, 0);
switchContent(currentFragment);
} else {
// change fragment by index
switchContent(0);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(CURRENT_FRAGMENT_KEY, currentFragment);
Log.d("onSaveInstanceState" ," current fragment" + currentFragment);
super.onSaveInstanceState(outState);
}
My manifest
:
<activity
android:name="com.appiphany.auskills.activity.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait" />
Everything is fine when I build my app with debug key: press Home button, then back to the app, it open previous fragment. But when I build on release mode (use my private key, I don't use proguard
), press HOME button in MainActivity, then open app again, it starts from SplashActivity. I have no idea with this strange issue. I event try this one but it doesn't help:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentFragment = savedInstanceState.getInt(CURRENT_FRAGMENT_KEY, 0);
switchContent(currentFragment);
}
Is there any ideas?
Update: I found another strange one: this issues only occurred when I install it from apk file. After install, phone will ask 2 options: Done
or Open
. If I press open
, this issues happened. When I kill app by task manager, then open again, it work correctly.