3

I am trying to clear every single activity on the stack by startActivity method with the Intent flags: (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY)

(I have an exit button that resets and finishes all activities and I tried all "how to make an exit button, how to terminate an app" questions/answers. They don't help me because of the singleInstance launchMode configuration of MainActivity.

Normally this should work and finish current activity automatically without calling finish(). However, as I marked MainActivity as singleInstance in the AndroidManifest.xml, it doesn't work. If I remove that singleInstance, it works as expected.

If I call finish() manually right after/before launching the MainActivity to clear all the stack, finish()will return the app to the previous activity on the stack. But I already launched the MainActivity. So everything gets messed.

(I tried adding Intent.FLAG_ACTIVITY_SINGLE_TOPand Intent.FLAG_ACTIVITY_MULTIPLE_TASK too just to see if it changes anything and it didn't)

I need a solution which works with singleInstance and which does not require manual finish().

Update 1: I try to launch this Intentfrom a SingleTop activity (but changing it changes nothing)

Update 2: I try this on Android 2.3.3 emulator

frankish
  • 6,738
  • 9
  • 49
  • 100
  • 1
    There is currently a bug that prevents FLAG_ACTIVITY_CLEAR_TOP from working under lollipop, as far as I know. Could this be the issue? –  Mar 10 '15 at 16:10
  • @jvrodrigues Thank you for this information, unfortunately I try this on Android 2.3.3 which is the minimum version I give support. – frankish Mar 10 '15 at 16:12
  • Hello @frankish did you find the solution i am stuck in same kind of situation..I want to clear all previous activities including activities having launchmode="singleInstance" – AbhayBohra Feb 01 '18 at 07:48

1 Answers1

4

You can try finishAffinity(), it closes all the previous activities from stack

Example:

Intent intent = new Intent(MainActivity.this, MyActivity.class);
finishAffinity()
startActivity(intent);