简体   繁体   中英

Android: How to find out when coming back from fragment

I have an app that uses fragments and when one fragment opens it changes the app title to a certain string, everything works fine but when I press the back button and go back to the main fragment the title doesn't change.

How can i find out (from the main fragment) if the app is coming back from a fragment?

If you already added your fragments into backstack, by using:

addToBackStack(null);

Then, when you back pressed from a fragment, you can add OnBackStackChangedListener for your FragmentManager:

getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                // check if the current fragment is the fragment you want.
                //then, updating your title.
            }
        });

I resolved using

getActivity().setTitle("title");

in the onDestroy() of the fragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM