Reputation: 864
here is the brief of my activities actions : Activity A is a launcher that call activity B with intent flag Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK. When Activity A launch activity B, ondestroy is called in activity A, as expected. When I press back button in activity B, ondestroy is called in activity B and it bring me to the hope launcher.
There are no alive activity at this moment, but the application is still visible and the task manager. When I click on it, it brings me to the activity A (maybe because it is the action MAIN in the manifest).
The behaviour I try to get is that when i press back button in activity B, it brings me to the home screen and I can return to is, as if it is root activity.
Can anyone help me for this ? Thanks you !
Upvotes: 0
Views: 872
Reputation: 59
If you click on the Back button in activity B, you will be able to navigate to the Home screen and return to the root activity(A) from the following sources.
why you used Intent.FLAG_ACTIVITY_CLEAR_TASK?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.btn_send)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Case 1.
//startActivity(new Intent(A.this, B.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
//Case 2.
startActivity(new Intent(A.this, B.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
}
});
}
FLAG_ACTIVITY_CLEAR_TASK
added in API level 11 int FLAG_ACTIVITY_CLEAR_TASK If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
Constant Value: 32768 (0x00008000)
Upvotes: 1
Reputation: 3313
You have to remove the flag FLAG_ACTIVITY_CLEAR_TASK, because as the android developer site here, the behavior will destroy your Activity A, You need to remove that flag and then your activity will remain in the stack, and you will be able to back to activity A
Upvotes: 0