导航抽屉后退按钮初级片段(Navigation drawer back button to prim

2019-10-24 01:08发布

看完这和Android文档中搜索时,我放弃了,决定要问在这里。 我想设置我的后退按钮Navigation Drawer ,使应用程序将从只关闭Home片段,以及任何其他片段将带我们回到Home 。 例如,如果我们开始在Home片段和导航到其他片段在导航抽屉里,当我们打的返回按钮,我们将得到Home片段,如果我们从首页片段回击,我们将退出应用程序。

我试图实现它是这样的:

public void onNavigationDrawerItemSelected(int position) {   
    ...
    // If the next fragment is Home, clear the backstack.
    if (frag instanceof Home) {
        getSupportFragmentManager().popBackStack();
        fragmentManager.beginTransaction().replace(R.id.container, frag)
                .commit();
    }
    // If the backstack already contains a transaction, replace the current fragment
    else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        fragmentManager.beginTransaction().replace(R.id.container, frag)
                .commit();
    } 
    // If the backstack is empty, insert the Home -> Fragment transaction
    else {
        fragmentManager.beginTransaction().replace(R.id.container, frag)
                .addToBackStack("").commit();
    }
    ...
}

    ...

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(Gravity.START)) {
        drawerLayout.closeDrawer(Gravity.LEFT);
    } else {
        super.onBackPressed();
        // If returning back to Home fragment, restore action bar title.
        if (getSupportFragmentManager().getBackStackEntryCount() > 0)
            onSectionAttached(0);
    }
}

我附上的链接无法正常工作,我实现的方式是不能持续工作。 例如,如果我从家里到其他片段导航,回到家里(不按后退按钮),我的片段得到互相重叠。

Answer 1:

我只是找到了一种方法,使这项工作相当容易,所以我决定把它张贴在这里:

// Are we in Home fragment?
private boolean homeFrag = true;
...
@Override
public void onNavigationDrawerItemSelected(int position) {
    ...
    // Next fragment is Home?
    if (frag instanceof Home)
        homeFrag = true;
    else
        homeFrag = false;

    fragmentManager.beginTransaction().replace(R.id.container, frag)
            .commit();
    onSectionAttached(position);
}
...
@Override
public void onBackPressed() {
    // Close navigation drawer if open
    if (drawerLayout.isDrawerOpen(Gravity.START)) {
        drawerLayout.closeDrawer(Gravity.LEFT);
    } else {
        // If not in Home, move back to Home.
        if (!homeFrag) {
            onNavigationDrawerItemSelected(0);
        } else
            super.onBackPressed();
    }
}

我想听听你对这个解决方案的想法。



Answer 2:

我觉得你打电话给super.onBackPressed()的方法onBackPressed应该在其他块中的最后一个代码。 其原因是,在方法调用可能会影响getBackStackEntryCount计数()。 而且已经有太多在我看来,这些调用。



Answer 3:

我做了这个方法taht管理片段和堆栈中为您服务。

/**
 * Change the current displayed fragment managing fragment backstack
 * 
 * @param frag the fragment to display
 * @param saveInBackstack true if you want the previous to be in backstack
 */
private void changeFragment(Fragment frag, boolean saveInBackstack) {
    String backStateName = ((Object) frag).getClass().getName();

    try {
        FragmentManager manager = getSupportFragmentManager();
        boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

        if (!fragmentPopped && manager.findFragmentByTag(backStateName) == null) { //fragment not in back stack, create it.
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.main_fragment_container, frag, ((Object) frag).getClass().getName());

            if (saveInBackstack) {
                Log.d(TAG, "Change Fragment : addToBackTack");
                transaction.addToBackStack(backStateName);
            } else {
                Log.d(TAG, "Change Fragment : NO addToBackTack");
            }

            transaction.commit();
        } else {
            Log.d(TAG, "Change Fragment : nothing to do");
            // custom effect if fragment is already instanciated
        }
    } catch (IllegalStateException exception) {
        Log.e(TAG, "Unable to commit fragment, could be activity as been killed in background. " + exception.toString());
    }
}

此外,在您的活动,覆盖onBackPressed:

@Override
public void onBackPressed() {
    int fragments = getFragmentManager().getBackStackEntryCount();
    if (fragments == 1) {
        finish();
    }
    super.onBackPressed();
}


文章来源: Navigation drawer back button to primary fragment