Set root activity / intent in Android

I switch between different actions using Intents. For one action, I would like it to clear the history stack, so when the user clicks the "Back" button, he accepts their "Home" instead of the previous actions in my application.

+3
source share
2 answers

I had to implement the same for my project. What I ended up with is replacing: startActivity (i); with startActivityForResult (i, UniqueId); in all the activities that I wanted to be part of the "story stack".

Then implemented:

setResult(UniqueId);
finish();

, "home/root".

:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == UniqueId && resultCode == UniqueId) {
 setResult(UniqueId);
 finish();
}
super.onActivityResult(requestCode, resultCode, data);
}

, "home/root" ( UniqueId), , "startActivityForResult (i, UniqueId)"; .

, ?

+6

finish() , startActivity?

, Android, onBackPressed() , startActivity() CLEAR_STACK.

+1

Source: https://habr.com/ru/post/1769588/


All Articles