2

Whenever I re-launch application from home screen it starts launcher activity specified in manifest but when I press back button then application does navigate to activity which was previously minimized by pressing home button:

home -> A -> B -> home -> A -> back -> B

Instead, I expect application should close activity A after pressing back button. Mentioned activities are specified in manifest file as follows:

<activity android:name="A"
          android:launchMode="singleTask"
          android:clearTaskOnLaunch="true"
          android:noHistory="true"
    <intent-filter>
        <action android:name="android:intent.action.MAIN"/>
        <category android:name="android:intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
<activity android:name="B"
          android:launchMode="singleTop"
</activity>

I need to clear the history of activity A whenever activity B is launched. This looks like clearTaskOnLaunch="true" does not work with noHistory="true" or when calling finish() manually on activity A. Additionally when either one of noHistory="true" or finish() are specified/called then it is necessary to have launchMode="singleTask" in manifest for activity A, otherwise activity B would be launched from home screen instead of activity A.

How can I clear activity B whenever activity A is launched? Adding finishOnTaskLaunch="true" to activity B in manifest does not help. Both activities do have default affinity. Compile sdk version is 26.

Darek Deoniziak
  • 763
  • 8
  • 17
  • 1
    you can use finishAffinity(); , this will finish all the previous activities – Nirmal Prajapat Jan 10 '18 at 09:24
  • Thanks, this solved the problem. I've added finishAffinity() calls to methods: onBackPressed(), onStop() and whenever I call intents to other activities from activity A. Activity A is a splashscreen, so I think it is fine to finishAffinity() whenever it is stopped. I've added finishAffinity() also to onBackPressed() because without it activity B was visible for little while when back was pressed on activity A. – Darek Deoniziak Jan 10 '18 at 10:08
  • You are probably seeing this long-standing nasty Android bug: https://stackoverflow.com/a/16447508/769265 Adding calls to `finishAffinity()` all over the place is a hack, and shouldn't be necessary, and you are probably just putting a band-aid on some other problem. Please don't do this. – David Wasser Feb 01 '18 at 13:03
  • Without the calls to `finishAffinity()` your app should return to the HOME screen when you press the BACK key in `A` (in the example you gave). The only reason it would go back to `B` is if you are seeing the nasty Android bug that I linked to in the previous comment. – David Wasser Feb 01 '18 at 13:05
  • 1
    To test my theory, try this: Remove all the calls to `finishAffinity()`. Install your app on the device and then go to Settings->App->YourApp and force stop it. Now launch your app from the HOME screen, you should see `A`. Go to `B`, you should see `B`. Press HOME. Launch your app again. You should see `A`. Press BACK. You should see the HOME screen and your app task should be gone. This is the correct default Android behaviour. – David Wasser Feb 01 '18 at 13:08
  • Also, why do you need to clear history of `A` when you launch `B`? Please explain what you are trying to do. It sounds unnecessarily complicated. – David Wasser Feb 01 '18 at 13:10
  • @DavidWasser Splash screen (A) first checks multiple stuff and then decides if client should Activate application or proceed directly to Main activity. This is something I cannot change as it's commercial app designed this way. Launching app from home screen must always start Splash screen. Sorry for late answer. – Darek Deoniziak Feb 12 '18 at 21:20
  • Did you test my theory as described? – David Wasser Feb 13 '18 at 07:20
  • @DavidWasser Not yet, but I will tomorrow. – Darek Deoniziak Feb 14 '18 at 17:24
  • 1
    @DavidWasser Yes, I can confirm your theory. Force stop does fix the problem. – Darek Deoniziak Feb 15 '18 at 13:05
  • Brilliant. So now you know! – David Wasser Feb 15 '18 at 14:21

0 Answers0