Replace current activity
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, a开发者_StackOverflow中文版nd 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?
Yes. It is fine to use api this way.
The proper way to achieve this is using 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 you are in an activity, otherwise if you are using fragments use getActivity()
This way, the activity is started, you properly set your hierarchy for your back button, and you also destroy the appropriate activity.
try using FLAG_ACTIVITY_TASK_ON_HOME
, FLAG_ACTIVITY_NEW_TASK
in the intent flags
You can add android:launchMode="singleInstance" in your activity, then override onNewIntent method to update date
Reference PlayerActivity in ExoPlayer Demo
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.
精彩评论