change navigation drawer selected item on fragment backstack change : <p>I have two fragments <code>FragmentHome</code> and <code>FragmentAbout</code>, I have added <code>NavigationDrawer</code> to app when I click <code>Home</code> it opens <code>FragmentHome</code> and <code>About</code> opens <code>FragmentAbout</code>, when I open <code>FragmentAbout</code> I am also adding it to backstack. This is working fine.</p>

<p>Now the problem is when I click on <code>About</code> and press back button it goes to the <code>FragmentHome</code> but the <code>NavigationDrawer</code> still shows the <code>About</code> as selected item, I want to change this selected item to <code>Home</code> when I press back button from <code>FragmentAbout</code></p>

<p>Home Activity:</p>

<pre><code>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();
    }
}}
</code></pre>

<p>FragmentHome</p>

<pre><code>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);
}
}
</code></pre>

<p><code>FragmentAbout</code> code is same as <code>FragmentHome</code> just layout change.</p>

<p>I have searched a lot on stackoverflow but didn't find any solution yet, so if someone know how to do this please tell me.</p>
