Android: Clear the back stack

前端 未结 30 1826
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:47

In Android I have some activities, let\'s say A, B, C.

In A, I use this code to open B:

Intent intent = new Intent(this, B.class);
startActivity(inte         


        
相关标签:
30条回答
  • 2020-11-22 07:24

    What about adding in manifests file for related activity :

    android:noHistory="true"
    

    to the activity definition of B and C ? They will not be added to the backstack. Not sure if that is what you want.

    0 讨论(0)
  • 2020-11-22 07:25

    Try adding FLAG_ACTIVITY_NEW_TASK as described in the docs for FLAG_ACTIVITY_CLEAR_TOP:

    This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

    So your code to launch A would be:

    Intent intent = new Intent(this, A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);
    CurrentActivity.this.finish(); // if the activity running has it's own context
    
    
    // view.getContext().finish() for fragments etc.
    
    0 讨论(0)
  • 2020-11-22 07:25

    Advanced, Reuseable Kotlin:

    You can set the flag directly using setter method. In Kotlin or is the replacement for the Java bitwise or |.

    intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
    

    If use this more than once, create an Intent extension function

    fun Intent.clearStack() {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    

    You can then directly call this function before starting the intent

    intent.clearStack()
    

    If you need the option to add additional flags in other situations, add an optional param to the extension function.

    fun Intent.clearStack(additionalFlags: Int = 0) {
        flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    
    0 讨论(0)
  • 2020-11-22 07:27

    I found an interesting solution which might help. I did this in my onBackPressed() method.

    finishAffinity();
    finish();
    

    FinishAffinity removes the connection of the existing activity to its stack. And then finish helps you exit that activity. Which will eventually exit the application.

    0 讨论(0)
  • 2020-11-22 07:28

    For future research, try this code.

    Intent intent = new Intent(context, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
    
    0 讨论(0)
  • 2020-11-22 07:29

    Try using

    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    and not

    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
    0 讨论(0)
提交回复
热议问题