我有两个片段FragmentHomeFragmentAbout,我在app中添加了NavigationDrawer当我单击Home时,它将打开FragmentHomeAbout打开FragmentAbout,当我打开FragmentAbout时,我也将其添加到backback。这很管用。
现在的问题是,当我单击About并按后退按钮时,它会转到FragmentHomeNavigationDrawer仍显示About为选定项,我想在从Home按后退按钮时将此选定项更改为FragmentAbout
家庭活动:

public class ActivityHome extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

Toolbar toolbar;

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

    // Toolbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Drawer layout
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.nav_drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.nav_drawer_open, R.string.nav_drawer_close);
    assert drawer != null;
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    // Navigation view
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;
    navigationView.setNavigationItemSelectedListener(this);

    // Open first menu item
    navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);

    // Set first item checked
    navigationView.setCheckedItem(R.id.nav_home);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    final MenuItem menuItem = item;

    // Check if menu item is selected
    if (item.isChecked())
        item.setChecked(false);
    else
        item.setChecked(true);

    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {

        // Open home fragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_layout, new FragmentHome())
                .commit();

    } else if (id == R.id.nav_about) {

        toolbar.setTitle("About");

        // Open home fragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_layout, new FragmentAbout())
                .addToBackStack(null)
                .commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.nav_drawer_layout);
    assert drawer != null;
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

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

碎片之家
public class FragmentHome extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.fragment_home_title);
}
}

FragmentAbout代码与仅更改布局相同。
我已经在stackoverflow上搜索了很多,但是还没有找到任何解决方案,所以如果有人知道怎么做,请告诉我。

最佳答案

有一个onbackstackchangedlistener可用于此。试过了,效果很好。

public Fragment getCurrentFragment() {
    return this.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
   ...
   this.getSupportFragmentManager().addOnBackStackChangedListener(
       new FragmentManager.OnBackStackChangedListener() {
          public void onBackStackChanged() {
              Fragment current = getCurrentFragment();
              if (current instanceof MyFragmentA) {
                 navigationView.setCheckedItem(R.id.nav_myfragA);
              } else {
                 navigationView.setCheckedItem(R.id.nav_myfragB);
           }
       }
   });
...
}

08-18 06:02