Replace current activities

I need to replace the current activity with a new one. That is, I want to start a new action and remove the current activity from the task stack.

Based on the documentation, it seems that the best way is to start working with Activity.startActivity as usual, and then call Activity.finish to immediately close the current activity.

Is this a valid use of these APIs or should I do something else?

+53
android android-activity
Jan 22 '10 at 9:14
source share
5 answers

Yes. It is good to use api in this way.

+55
Jan 22 '10 at 9:22
source share

The correct way to achieve this is to use the following:

Intent intent = new Intent(this,MyActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME); startActivity(intent); this.finish(); 

The code assumes that you are in action, otherwise, if you use fragments, use getActivity ()

Thus, the action starts, you correctly set your hierarchy for your return button, and also destroy the corresponding activity.

+27
Nov 09 '15 at 19:10
source share

try using FLAG_ACTIVITY_TASK_ON_HOME , FLAG_ACTIVITY_NEW_TASK in the FLAG_ACTIVITY_NEW_TASK intent

+5
Aug 18 2018-11-11T00:
source share

You can add android: launchMode = "singleInstance" to your activity, then override the onNewIntent method to update the date

PlayerActivity Link in ExoPlayer Demo

+4
Apr 17 '16 at 8:29
source share

You can use FLAG_ACTIVITY_CLEAR_TASK when starting an action. I also defined launchMode for my activity in the manifest as singleTask, but that was because I wanted this behavior for a new activity. I think you can get what you want with respect to clearing the previous activity, no matter what you use for launchMode with your new activity, if you pass the startActivity flag FLAG_ACTIVITY_CLEAR_TASK.

0
Jan 27 '19 at 13:35
source share



All Articles