【问题标题】:Android back stack one fragment, when pressing hardware back button shows the back stacked one.Android 回栈一个片段,当按下硬件返回按钮时显示回栈一个。
【发布时间】:2016-10-25 07:47:53
【问题描述】:

Android 回栈一个片段,当按下硬件返回按钮时显示回栈一个。

以下是我的预期场景:

我有一个带有导航抽屉的活动。每个导航元素在点击时导航到不同的片段。我只想回栈主片段。当从任何其他片段按下返回按钮时,导航到主片段。从主页片段按下返回按钮时,应用程序退出。

【问题讨论】:

    标签: android android-fragments navigation-drawer onbackpressed


    【解决方案1】:

    你可以在这个方法中处理onBackPressed方法,注释//super.onBackPressed();这一行

        @Override
            public void onBackPressed() {
    //            super.onBackPressed();
    //Now check if current fragment is not home fragment
    // then replace current fragment with home fragment
    //if current fragment is home fragment then execute the following code
    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        startActivity(homeIntent); 
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2015-08-26
      • 2017-08-03
      相关资源
      最近更新 更多