Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes the activity stack

后端 未结 10 856
遇见更好的自我
遇见更好的自我 2020-12-17 09:20

I am developing the application in which i want to close whole application on button click. I know in android we should not think about to close the application because andr

相关标签:
10条回答
  • 2020-12-17 09:51

    This finally worked for me 100% of the time. I tried all of the flags standalone, never worked for all instances of the code. Used all the flags everywhere I want this functionality, about 5 different places, and it now works.

                Intent intent = new Intent(actvSignIn.this, actvNearbyPlaces.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
    
    0 讨论(0)
  • 2020-12-17 09:53

    setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    apart from that set launchMode to "singleTop" in FinishActivity definition in xml, overwrite onNewIntent method , you can pass some additional information as part of intent , instated of finishing your activity in onCreate finish it in onNewIntent method of activity based on some signal from calling activity or simply finish it based on your need . It maybe possible your other activities have different lauchmodes that's why they are not finishing .

    0 讨论(0)
  • 2020-12-17 09:57

    Give this a try. This should clear your activity stack.

    Intent i = new Intent(this,MyActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    
    0 讨论(0)
  • 2020-12-17 09:57

    As other answers have the following should work.

    Intent intent = new Intent(Activity3.this, FinishActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    

    For API level < 11, Intent.FLAG_ACTIVITY_CLEAR_TASK is not available. Instead I used IntentCompat.FLAG_ACTIVITY_CLEAR_TASK from support library.

    See IntentCompat

    Might help someone stumbling across.

    0 讨论(0)
  • 2020-12-17 10:04

    Try this:

    Intent intent = new Intent(Activity3.this, FinishActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
    
    0 讨论(0)
  • 2020-12-17 10:04

    Also you can use finishAffinity() function in last activity like:

    finishAffinity()
    startHomeActivity()
    

    Hope it'll be useful.

    0 讨论(0)
提交回复
热议问题