回到Android上的onBackPressed上的上一个片段

时间:2016-11-11 14:41:16

标签: android android-fragments onbackpressed

我是Android开发的新手,并使用Android的默认导航抽屉布局开发应用程序。

我在应用程序中使用片段。

我希望在下一个片段上按下后退按钮时返回上一个片段。 onBackPress必须实现以下功能

  1. 在下一个片段
  2. 时返回上一个片段
  3. 导航抽屉打开时关闭抽屉
  4. 在fragment1
  5. 中双击后退出应用程序

    这是我在MainActivity中的代码     在创建新片段时

    // replacing the fragment
    if (fragment != null) {
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
         ft.replace(R.id.content_frame, fragment);
      ft.commit();
    }
    

    onBackPress

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else if (getFragmentManager().getBackStackEntryCount() > 0) {
            FragmentManager fm = getSupportFragmentManager();
            fm.popBackStack();
        } else {
            super.onBackPressed();
        }
    }
    

    但这不起作用,在退回时退出应用程序,除非导航抽屉打开时它已关闭。

3 个答案:

答案 0 :(得分:1)

您没有将片段添加到Backstack。你只需要做

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
 .replace(R.id.content_frame, fragment);
 .addToBackStack(null);
 .commit();

由于Backstack实际上是由后退按钮处理的,因此您无需在onBackPressed()中处理此方案,因此您可以将其更改为:

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

答案 1 :(得分:0)

您必须将Fragment中的FragmentTransaction添加到后台。

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.replace(R.id.content_frame, fragment).addtoBackStack(null).commit();

通过提供标记null,它们都会按该标记进行分组。因此,如果您需要将它们全部关闭,您可以这样做或导航到某个。

我也不会使用replace(),因为那个背包非常错误。

尝试使用remove()然后使用add()

答案 2 :(得分:0)

您应该将片段添加到后台堆栈并以这种方式修改代码:

// this variable is needed to know how many times the back key has been pressed.
// It is incremented and reset accordingly to the logic you described
int mBackPressCount = 0;

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (getFragmentManager().getBackStackEntryCount() == 0) {
            // if there are no fragments in your back stack check the mBackPressCount
            if (mBackPressCount++ > 0) {
                // if you pressed back for the second time this line will terminate the activity
                super.onBackPressed();
            } 
        } else {
            // this line will pop the fragment from the back stack,
            // no need to pop it manually
            super.onBackPressed();
        }
    }
}

将片段添加到后台堆栈:

if (fragment != null) {
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.replace(R.id.content_frame, fragment);
     ft.addToBackStack(null);
     ft.commit();

     // when you add a fragment you should reset the back press count
     mBackPressCount = 0;
}

打开和关闭抽屉时重置mBackPressCount。如果您按下后退键一次并打开抽屉,则计数器必须再次设置为0。

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        mBackPressCount = 0;
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        mBackPressCount = 0;
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }
});