tatarusanu1
tatarusanu1

Reputation: 67

Android Navigation Drawer wrong toolbar title onResume

I have a Navigation Drawer in the main activity of my app. On the onCreate method of the activity I initialize one of the fragments like this :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_history);
        openFragment(menuItem);
    }

public void openFragment(MenuItem menuItem){
    Fragment newFragment = null;

    switch (menuItem.getItemId()){
        case R.id.menu_history :
            newFragment = new HistoryFragment();
            break;
        //.....
    }

    if (newFragment != null){
        //Replace content frame in activity_main.xml with newFragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, newFragment)
                .commit();

        menuItem.setChecked(true);
        getSupportActionBar().setTitle(menuItem.getTitle());
    }

    drawerLayout.closeDrawers();
}

This all works well, the fragment appears on startup with the title on the toolbar being "History". But when the app goes into onPause and then onResume the toolbar title switches from "History" to the app name. I suspect this is an issue with onResume not opening the fragment / returning to its previous state correctly, because when I added the following lines to onResume the issue stopped:

@Override
protected void onResume() {
    super.onResume();
    MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_history);
    openFragment(menuItem);
}

That solution seems to fix it but that means it has to reload the fragment with the animations every time the app is resumed, which isn't optimal. Any ideas on how to fix this?

If it helps, to recreate the issue I found useful to make the app split screen, because it always calls onResume. Thanks.

Upvotes: 0

Views: 936

Answers (2)

Ben P.
Ben P.

Reputation: 54194

Inside your openFragment() method, you write:

    getSupportActionBar().setTitle(menuItem.getTitle());

This is the only thing that changes your toolbar's title.

Note that this code has nothing to do with your Fragments, per se. When your activity is destroyed and recreated, the active Fragment will be successfully (automatically) destroyed and recreated by the FragmentManager... but your openFragment() method won't run again and so nothing will update your toolbar's title.

There are numerous ways you could solve this. Probably the right thing to do is update your toolbar's title from within one of your Fragment's lifecycle methods.

Edit: A reasonable place to update your toolbar's title would be in your fragment's onActivityCreated() method. This will run both when the fragment is first added and during recreation. Something like:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("hello world");
}

Upvotes: 1

Christian
Christian

Reputation: 4641

If you do not want to re-create the fragment each time, you should use saveInstanceState like mentioned here: https://stackoverflow.com/a/17135346/1505074

Upvotes: 0

Related Questions