简体   繁体   中英

android fragments - if i add two fragments on top of each other what is callback when top one is removed?

when i have two fragments that are stacked on top of each other in a framelayout using "add" transaction how can i know what the call back is when one is removed.

so imagine i have a framelayout like this:

<FrameLayout
                            android:id="@+id/fl_cart_address"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:elevation="4dp"/>

and then i add two fragments like this:

mFragment = new ExampleOneFragment();
              FragmentTransaction ft = getFragmentManager().beginTransaction();
              ft.add(R.id.fl_cart_address, mFragment).commit();

              mFragment = new ExampleTwoFragment();
                          FragmentTransaction ft = getFragmentManager().beginTransaction();
                          ft.add(R.id.fl_cart_address, mFragment).commit();

now if i hit the back button then fragmentTwo is gone. but what call back can i get in FragmentOne so i know that its actually gone ? Basically i just need a callback when the top fragment gets popped off the backstack. I tried onResume but its not working.

use FragmentManager.OnBackStackChangedListener

Register a callback to using FragmentManager.addOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener listener)

it will send you a callback when the Backstack is changing.

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