【问题标题】:Switching between fragments with back button使用后退按钮在片段之间切换
【发布时间】:2013-12-16 00:27:45
【问题描述】:

好的,我有一个包含一个主要片段的活动,上面有一个菜单。当用户点击一个菜单项时,另一个片段会在屏幕上显示动画,使用以下代码:

FragmentTransaction ft = getFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
ft.hide(getFragmentManager().findFragmentByTag("menu_fragment"));

Fragment opisFragment = getFragmentManager().findFragmentByTag("opis_fragment");
if (opisFragment == null) {
    opisFragment = new OpisFragment();
    ft.add(R.id.p_container, opisFragment, "opis_fragment");
    ft.commit();
} else {
    ft.show(opisFragment);
}

注意:pr_fragment 是当前fragment的标签,即有菜单的那个。

现在,这很好用,但是当我在第二个片段上时,我想添加功能,当用户单击后退按钮时,它将显示第一个片段。使用此代码,当我单击返回时,它会一起退出活动。 感谢您的帮助!

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    您只需要使用addToBackStack(String name) of FragmentTransaction

    // Showing menu fragment also added in backstack
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    
    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
      .add(R.id.p_container, menuFragment, "menu_fragment")
      .addToBackStack("menu_fragment")
      .commit();
    
    
    // Showing opis fragment also added in backstack
    FragmentTransaction ft2 = getFragmentManager().beginTransaction();
    
    ft2.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
          .add(R.id.p_container, opisFragment, "opis_fragment")
          .addToBackStack("opis_fragment")
          .commit();
    

    假设“opis fragment”在前台,当你按下返回键时,“menu_fragment”将显示回到前台,再次按下返回键将退出活动。

    【讨论】:

    • 哦,哇,这太简单了。还有一件事,当我从 opis_fragment 转到 menu_fragment 时如何添加动画(使用后退按钮)?
    • setCustomAnimations(int enter, int exit, int popEnter, int popExit) of FragmentTransaction 将完成这项工作。
    • 另见question
    【解决方案2】:

    With this code, when i click back it exits the activity alltogether.

    正常,因为您的应用堆栈中只有您的活动。 addToBackStack() 方法就是你要找的。​​p>

    if (opisFragment == null) {
        opisFragment = new OpisFragment();
        ft.add(R.id.p_container, opisFragment, "opis_fragment");
        ft.addToBackStack("tag"); // <<< this line
        ft.commit();
    }
    

    来自doc

    Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

    【讨论】:

      【解决方案3】:

      在 mainactivity 中,您可以检查片段数,如果片段数超过一个,我们将显示返回按钮

      if(getSupportFragmentManager().getBackStackEntryCount() > 0)
                  {
                     mDrawerToggle.setDrawerIndicatorEnabled(false);
                      getSupportActionBar().setDisplayShowHomeEnabled(false);
                      getSupportActionBar().setHomeButtonEnabled(true);
                      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                  }
                  else
                  {
                      mDrawerToggle.setDrawerIndicatorEnabled(true);
                      getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                      getSupportActionBar().setHomeButtonEnabled(false);
                      getSupportActionBar().setDisplayShowHomeEnabled(true);
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 2012-12-28
        • 2019-12-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2018-01-24
        相关资源
        最近更新 更多