Android: Clear the back stack

前端 未结 30 1757
被撕碎了的回忆
被撕碎了的回忆 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: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.
    

提交回复
热议问题