导航到后退按钮preSS菜单 [英] Navigate to menu on back button press

查看:97
本文介绍了导航到后退按钮preSS菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从浏览:

  • 主要活动活动2
  • 活动2 活动3
  • 活动3 活动4
  • Main activity to Activity 2
  • Activity 2 to Activity 3
  • Activity 3 to Activity 4

意图

我还创建了菜单,使用户可以直接从活动4 导航到主要活动
但是从浏览活动4 后主要活动通过使用菜单,当我preSS回来,它带我到活动3 ,而不是退出应用程序

I've also created the menu so that user can directly navigate from Activity 4 to Main activity.
But after navigating from Activity 4 to Main activity by using menu, when I press back, it takes me to Activity 3 rather than exiting the application.

我想:

@Override
public void onBackPressed() {
    super.onBackPressed();
    MainActivity.this.finish();
}

但没有收获。有什么建议?

But no gain. Any suggestions?

推荐答案

刚刚尝试这个code,当你从4日活动主活动导航。

Just try this code when you navigate from 4th Activity to Main Activity.

Intent inMain=new Intent(Activity4.this, MainActivity.class);
inMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inMain);

请注意:

这code你的情况,清除previous活动,并启动的主要活动与在backstack没有任何活动。

This code in your case, clears the previous activities and launches the main activity with no activity in the backstack.

这篇关于导航到后退按钮preSS菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆