Android - Clearing Navigation Backstack

后端 未结 3 1095
隐瞒了意图╮
隐瞒了意图╮ 2021-01-20 18:34

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

相关标签:
3条回答
  • 2021-01-20 18:46

    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);
    
    0 讨论(0)
  • 2021-01-20 18:55

    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 with FLAG_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.

    0 讨论(0)
  • 2021-01-20 18:56

    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.

    0 讨论(0)
提交回复
热议问题