Reputation: 1
I think this question has been asked several times,but this is a different scenario.I have 4 activities say act1,act2,act3,act4 and i had put intends such that act1->act2->act3->act4
act1-branch selection,
act2-year selection,
act3-subject selection,
act4-displays the selected subject
the user can press the back button from act4 it moves to act3 and can select different subject,similarly when he presses back button on act3,it moves to year selection activity i.e
act2.so i want the 3 activities act1,act2,act3 to be alive so i did n't write the finish method.i had put home button in act4 that leads to act1.
when i press back button in act1 the app should close,but its forming a loop and all the prevoius activities opened are appearing instead.please,provide me the solution sorry if this question is too lengthy
Upvotes: 0
Views: 88
Reputation: 3147
That's how it should work. Now in your stack you have act1 -> act2 -> act3 -> act4 -> act 1.
So when you press back button from act1 it opens only act4. What you have to do is, when starting act1 from act4 add FLAG_ACTIVITY_CLEAR_TOP
flag to your intent.What this flag does is, asks the android os to remove the previous activities from back stack.
Upvotes: 0
Reputation: 1468
You should close all activities when coming back to activity 1 from activity 4, like the following code, using the flag Intent.FLAG_ACTIVITY_CLEAR_TOP
:
Intent it = new Intent(Activity4.this, Activity1.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
So in your activity 1 when you press the back button, it will finish the app.
Hope it helps!
Upvotes: 2
Reputation: 117
In app1 add:
@Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
}
Upvotes: 0
Reputation: 2877
This may not be the best way..but you can override the onBackPressed button of your act1 and write
System.exit(0);
This will close the app instantly. There may be better solution too
Upvotes: -1