I have 4 pages.
From page_1 > page_2 > page_3 > page_4.
Once the user reaches page_3 and clicks a button, it navigates to page_4. Once the button is clicked
for all the other activities like page_3,page_2,page_1,
Use FLAG_ACTIVITY_NO_HISTORY(If set, the new activity is not kept in the history stack.) :
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
I'm not sure that these methods will works for you. The first method is by adding FLAG_ACTIVITY_TASK_ON_HOME
when you go to page_4 from page_3:
Intent intent = new Intent(this, page_4.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
So once you press BACK button within page_4, it directs you to HOME activity (MainActivity) first, then you can press BACK button again to exit the app from this activity.
From the docs:
If set in an Intent passed to
Context.startActivity()
, this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction withFLAG_ACTIVITY_NEW_TASK
.
The second way is, set android:noHistory="true"
within the activity in manifest. Apply this attribute for page_1 till page_4. But this method has two disadvantages. First, your activity is completely has no back stack. The second, the activities you set with this attribute get destroyed once you press HOME button or when you get an incoming call. I never found this topic, so please CMIIW.
Try set Intent.FLAG_ACTIVITY_NO_HISTORY And Intent.FLAG_ACTIVITY_TASK_ON_HOME
From documentation Intent.FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
Intent.FLAG_ACTIVITY_TASK_ON_HOME
If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.