DomeWTF
DomeWTF

Reputation: 2420

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?

Upvotes: 0

Views: 878

Answers (2)

Giang Nguyen Truong
Giang Nguyen Truong

Reputation: 31

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.
            }
        });

Upvotes: 1

DomeWTF
DomeWTF

Reputation: 2420

I resolved using

getActivity().setTitle("title");

in the onDestroy() of the fragment

Upvotes: 0

Related Questions