3

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.

ductran
  • 10,043
  • 19
  • 82
  • 165

1 Answers1

2

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

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
  • This fix will probably cause you other problems. See my comment and read about this Android framework bug. – David Wasser Nov 06 '13 at 09:30
  • Thanks for mention. I tried your way and it worked. But I misunderstand a bit. I just wonder is there any problem with this way? Because I see it check if not task root then finish my activity. For example, my app is run from installer -> Splash, when Splash isn't task root, it will finish, then it will create new task from Splash? – ductran Nov 06 '13 at 15:10
  • If your splash screen is NOT the root activity, then it will finish(). This will drop the user into the activity underneath the splash screen (which is an activity in your application that was launched before). It doesn't create a new task. The problem is that if there is already a task containing your app and you launch it from the HOME screen, Android creates another instance of your splash screen activity and puts that on top of the existing task. This isn't what you want. What you want is that it just brings the existing task forward, which is what this workaround does. – David Wasser Nov 06 '13 at 15:16