简体   繁体   中英

Can the views of a fragment be modified when the fragment is in backstack?

I have two fragments which share the same spot on a activity. When activity starts fragA is displayed. When you click a button from fragA it gets pushed to backstack and replaced by fragB . When a button from fragB is pressed the fragB is destroyed and fragA is poped from the backstack but it should also change some views based on what button from fragB was pressed (set the text for a textview for example). I used interfaces for fragment-activity communication. Everything goes well but the views from fragA don't want to change. I think I'm trying to change them while the fragment is in backstack.

@Override
    public void onFragBClick(Bundle bundle) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragA = (FragA) fragmentManager.findFragmentByTag(TAG_FRAG_A);
        if (fragA != null) {
            fragmentManager.popBackStack(TAG_FRAG_A_BACKSTACK, 
                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
            fragA.onFragBSignalReceived(bundle);
        }

    }

This is the method that handles communication. Everything seems fine in logs, onFragBSignalReceived gets called, but I can't change the state of fragA 's views in it. My guess is that when it gets called fragA is still on the backstack and that's why views are not updated. In that case how can I achieve this behavior in some other way?

You might want to use popBackStackImmediate() instead of popBackStack(). popBackStackImmediate() will perform the operation inside the method, whereas popBackStack() will queue the action and perform it later on.

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