Handling ActionBar title with the fragment back stack?

前端 未结 11 1467
挽巷
挽巷 2020-12-07 13:10

I have an Activity where I load in a ListFragment and, upon clicking, it drills down a level and a new type of ListFragment is shown,

11条回答
  •  感情败类
    2020-12-07 13:50

    As the original answer is quite old, this might come of help as well. As the documentation states, one might want to register a listener to listen on the back stack changes in the hosting Activity:

    getSupportFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {
                    // Update your UI here.
                }
            });
    

    Then, identify the situation in the callback method and set a proper title, without accessing the ActionBar from the Fragment.

    This is a more elegant solution as the Fragment doesn't have to know about the ActionBar existence and Activity is usually the place that is managing the backstack so having it handled over there seems to be more appropriate. Fragment should at all time be considered only by its own content, not the surroundings.

    More on the topic in the documentation.

提交回复
热议问题