0

I have a problem with an activity which is started with FLAG_ACTIVITY_REORDER_TO_FRONT is not recreated and present on the screen after FLAG_ACTIVITY_CLEAR_TASK.

So the step is:

  1. Open app, enter Welcome Activity.
  2. Finish Welcome activity and start activity A without specific intent flag.
  3. Start activity B with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A (new instance of B is created and now stack became A->B).
  4. Start activity A from B with FLAG_ACTIVITY_REORDER_TO_FRONT (existing A instance brought to top of stack so stack became B->A).
  5. And under some condition, I need to start over from the beginning (just like another normal app launch), so started Welcome activity using FLAG_ACTIVITY_CLEAR_TASK.

So the app will enter phase 2 again after phase 1, which is what I expected, but then, if I try to start activity B again with FLAG_ACTIVITY_REORDER_TO_FRONT from activity A, there is activity B's callback 'onNewIntent, onStart, onResume' in a row, but it doesn't present on the screen. It looks like to me that there is still the previous instance of activity B somewhere but not showing to me.

I don't specify launch mode for either activity A or B.

I know that document says about FLAG_ACTIVITY_CLEAR_TASK that "This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.". And use them together does solve my problem, but then if I click home button to put app background and then launch again, it will become another app launch (enter phase 1) but not back to the previous top activity.

So my questions are:

  1. When I use FLAG_ACTIVITY_CLEAR_TASK to start welcome activity, I don't see onDestroy of either activity A or B, is it by design?
  2. If they are not destroyed, where do they stay and can I have a reference to them?
  3. How can I make activity B presented on the screen after all of these steps?

Code for phase 5:

Intent i = new Intent(A.this, WelcomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Thanks in advance.

AndroidManifest.xml

 <activity
            android:name=".activity.WelcomeActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity
            android:name=".activity.A_Activity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            android:theme="@style/BaiduMapTheme.MainMap"
            android:windowSoftInputMode="adjustPan" />

<activity
            android:name=".activity.B_Activity"
            android:configChanges="locale|fontScale|keyboard|keyboardHidden|layoutDirection|mcc|mnc|navigation|orientation|screenLayout|screenSize|touchscreen|uiMode"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateVisible" />
always_beta
  • 229
  • 4
  • 10
  • add one more flag FLAG_ACTIVITY_CLEAR_TOP – Arjun saini Aug 23 '16 at 06:10
  • Sounds very strange. You can use `adb shell dumpsys activities activity` to see the tasks and the list of activities within them. Please walk through your scenario up to the part where you start the `WelcomeActivity` using `FLAG_ACTIVITY_CLEAR_TASK` and then check the task list and see what activities are in the task list. – David Wasser Aug 23 '16 at 15:20
  • @Er.Arjunsaini from my understanding, FLAG_ACTIVITY_CLEAR_TOP will clear all activities that are on top of the existing target activity instance, and FLAG_ACTIVITY_CLEAR_TASK will clear all activities, what does this mean to use them both at the same time? – always_beta Aug 27 '16 at 06:49
  • @DavidWasser With only FLAG_ACTIVITY_CLEAR_TASK, the Activity B will still be in the stack, but as the document says, I think I should not use FLAG_ACTIVITY_CLEAR_TASK alone but use with FLAG_ACTIVITY_NEW_TASK. I updated my conclusion and final solution as the answer, please check it. – always_beta Aug 27 '16 at 08:59
  • I assume that if you use `FLAG_ACTIVITY_CLEAR_TASK` without also using `FLAG_ACTIVITY_NEW_TASK`, it is just ignored. However this still does not explain the behaviour you described. Can you post your manifest? – David Wasser Aug 27 '16 at 20:27

1 Answers1

0

OK, now I think I have those questions because of my incorrect using of the intent flags.

First of all, I should use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK at the same time as the document says.

Then everything looks normal and as expected, except one that: if I put the app into background and then click the launch icon again, the WelcomeActivity will be created again.

Previously I thought my existing activity stack is cleared and a new WelcomeActivity is created, but it turns out the existing activity stack is still there, but an extra WelcomeActivity is created, so what I need to do is add some extra flag in WelcomeActivity to determine if it's created in this condition, if yes, then just call finish() in onCreate(), then I can back to the previous activity I was in before enter background.

always_beta
  • 229
  • 4
  • 10
  • 2
    Actually, you are probably seeing a long-standing nasty Android bug, if your root `Activity` is getting created again if you put your app to the background and then press the launch icon again :-( This happens only if you launch your app for the first time from an IDE or directly from the installer (you press the "open" button after installing or updating the app). To see if this is the problem, don't launch your app using an IDE or from the installer. Just install your app and then open it for the first time by tapping the launch icon. The do something, put the app in the background and... – David Wasser Aug 27 '16 at 20:21
  • ...launch it again from the launch icon. You should NOT see another instance of your root `Activity`. If you do, then you are seeing a different problem. See http://stackoverflow.com/a/16447508/769265 for a description of the Android bug. – David Wasser Aug 27 '16 at 20:24
  • 1
    @DavidWasser so it's a bug of android.. for my case, I tried 1. directly launch through android studio (v2.1), and 2. stopped the debugger and launch without debugger attached, and 3. also tried built an apk and install into device and launch, and everytime the root activity is created on my device (os v4.4.2, and I posted my manifest of those activities). But anyway now it's cleared that it's not by design :( thanks for clarification here :) – always_beta Aug 29 '16 at 16:17
  • 1
    @DavidWasser Thanks! You saved me some hours for pointing out this bug. :) – Luiz Jun 18 '19 at 14:19