Replace current activity

Viewed 30253

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

Based on the documentation, it seems the best way would be to start the activity using Activity.startActivity as per usual, and then call Activity.finish immediately to close the current activity.

Is this a valid usage of these APIs or should I be doing something else?

5 Answers

Yes. It is fine to use api this way.

You can use FLAG_ACTIVITY_CLEAR_TASK when you start the activity. I also defined the launchMode for my activity in the manifest as singleTask, but that was because I wanted that behavior for the new activity. I think you can get what you want with regard to clearing the previous activity regardless of what you use for launchMode with your new activity, as long as you pass startActivity the flag FLAG_ACTIVITY_CLEAR_TASK.

Related