Handling ActionBar title with the fragment back stack?

前端 未结 11 1468
挽巷
挽巷 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:43

    In every fragment and every activity I change the title like this. This way the active title will always be correct:

    @Override
    public void onResume() {
        super.onResume();
        // Set title
        getActivity().getActionBar()
            .setTitle(R.string.thetitle);
    }
    

    There is some cases where onResume isn't called inside fragments. In some of these cases we can use:

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser) {
            // Set title
            getActivity().getActionBar()
                .setTitle(R.string.thetitle);
        }
    }
    

提交回复
热议问题