在我的应用程序中,我有NavigationView,并在此NavigationView中设置了用于打开某些fragments的菜单!

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="320dip"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#000"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/tester_drawer_header"
    app:itemIconTint="#fff"
    app:itemTextColor="#fff"
    app:menu="@menu/tester_drawer_main"/>


我想要在单击此菜单时打开一个fragment,并且要在转到嵌套fragments并按onBack按钮时打开第一个片段,而不打开backStack fragments

我写下面的代码,但是单击后从button回到fragments回到backStack,但是我只想先打开fragment

MyActivity代码:

public class TesterDashboardActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tester_dashboard);

        loadFragment(new TesterTestListFragment(), userName);
}

    @Override
    public void onBackPressed() {
        fragmentBackCount = getSupportFragmentManager().getBackStackEntryCount();

        if (fragmentBackCount <= 1) {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                //finishAffinity();
                super.onBackPressed();
            }
        } else {
            getSupportFragmentManager().popBackStack();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.nav_home:
                loadFragment(new TesterTestListFragment(), userName);
                break;
            case R.id.nav_billing:
                loadFragment(new TesterFinanceFragment(), context.getString(R.string.menu_billing));
                break;
            case R.id.nav_edit_profile:
                loadFragment(new TesterProfileFilterEditFragment(), context.getString(R.string.menu_edit_profile));
                break;
            case R.id.nav_bank:
                loadFragment(new TesterShebaFragment(), context.getString(R.string.menu_bank));
                break;
            case R.id.nav_report:
                loadFragment(new TesterReportsFragment(), context.getString(R.string.menu_report));
                break;
            case R.id.nav_setting:
                loadFragment(new TesterSettingFragment(), context.getString(R.string.menu_setting));
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void loadFragment(Fragment fragment, String toolbarTitle) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.testerFragment, fragment,TAG_FRAGMENT);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        toolbar.setTitle(toolbarTitle);
    }


我该如何解决?

最佳答案

在您的代码中,我发现fragmentBackCount>1只需调用一次getSupportFragmentManager().popBackStack();一次,因此您将在Backstack上看到顶部片段。
 我认为您应该调用getSupportFragmentManager().popBackStack();,fragmentBackCount-1时间到达第一个fragmnet

 else {
       for(int i=0;i<fragmentBackCount-1;i++)
           getSupportFragmentManager().popBackStack();
      }

09-08 06:53