Activity be killed when press HOME only in release mode

混江龙づ霸主 提交于 2019-12-31 01:46:34

问题


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.


回答1:


Issue has been fixed by using FLAG_ACTIVITY_NEW_TASK when navigate from Splash to Main activity. I just wonder why it can work in debug mode (Reproduce install from apk like release mode). Here is my workable code for anyone has same issues:

 Intent intent = new Intent(SplashActivity.this, MainActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 finish();
 startActivity(intent);

Update: many thanks to David Wasser, so the right answer should like this:

// SplashActivity
 if(!isTaskRoot()) {
    finish();
    return;
 }

Reference: Re-launch of Activity on Home button, but...only the first time



来源:https://stackoverflow.com/questions/19782485/activity-be-killed-when-press-home-only-in-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!