Search code examples
androidnavigation-draweronbackpressed

Android NavigationView change title onBackPressed


I have a NavigationView that changes certain Fragment by clicking on its item and setting toolbar's title with item's name:

fragmentTransaction = getFragmentManager().beginTransaction();
    int id = item.getItemId();
    switch (id){
        case R.id.studying:
            fragmentTransaction.replace(R.id.container, studyFragment).addToBackStack(null);
            break;
        case R.id.events:
            fragmentTransaction.replace(R.id.container, eventsFragment).addToBackStack(null);
            break;
        case R.id.discount:
            fragmentTransaction.replace(R.id.container, discountsFragment).addToBackStack(null);
            break;
        case R.id.bookmarks:
            fragmentTransaction.replace(R.id.container, bookmarksFragment).addToBackStack(null);
            break;
        case R.id.settings:
            fragmentTransaction.replace(R.id.container, settingsFragment).addToBackStack(null);
            break;
        case R.id.prolong_sign:
            fragmentTransaction.replace(R.id.container, prolongFragment).addToBackStack(null);
            break;
        case R.id.contacts:
            fragmentTransaction.replace(R.id.container, contactsFragment).addToBackStack(null);
            break;
    }
    toolbarTitle.setText(item.getTitle().toString());
    fragmentTransaction.commit();

Proving better usability I override onBackPressed method:

 @Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        FragmentManager fm = getFragmentManager();
        if (fm.getBackStackEntryCount() > 0 && fm.getBackStackEntryCount() != 1) {
            fm.popBackStackImmediate();
        } else if (fm.getBackStackEntryCount() == 1) {
            toolbarTitle.setText("TEST");
            fm.popBackStackImmediate();
        } else {
            super.onBackPressed();
        }
    }
}

If I back to the first Fragment, my toolbarTitle sets okay according to:

 else if (fm.getBackStackEntryCount() == 1) {
            toolbarTitle.setText("TEST");
            fm.popBackStackImmediate();
        }

But I don't know how to make toolbarTitle to change title correctly on each onBackPressed and make previous item in NavigationView checked. Can you help me with it?

EDIT

I created an interface

public interface OnToolbarTitle {
void setTitle(String title);

}

implemented it MainActivity and overrided

 @Override
public void setTitle(String title) {
    toolbarTitle.setText(title);
}

And tried to use it in Fragments by attaching it in Fragments:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    titleSetter = (OnToolbarTitle) context;
}

@Override
public void onResume() {
    super.onResume();
    titleSetter.setTitle("Test-frag3");
}

But I got NullPointerException in line with setTitle() method.

EDIT

Ok I solved my problem by accessing Toolbar in fragment.

    View view = getActivity().findViewById(R.id.toolbar);
    TextView title = (TextView) view.findViewById(R.id.toolbar_title);
    title.setText("study lol");

Still, I'd be glad to see, how can I achive it with interface.


Solution

  • You should consider making your fragments setting the title. I.e. define interface like:

    interface HostContract {
        setTitle(String title);
    }
    

    and make your activity implement it. Then in each fragment you should call ((HostContract)getActivity).setTitle() in fragment's onResume(). This way you set the title when your fragment goes into game (be it because it is newly added, or because you took previous one from backstack, which should solve your problem. And that's basically all you need to do.