有没有一种方法可以检查片段中的popBackStack调用

时间:2018-10-04 11:48:50

标签: java android

我有几个碎片的后叠,我需要知道我已经从另一个碎片中返回了。

类似于您有onActivityResult时的情况,我想知道您是否可以检查片段中的popBackStack()呼叫?

我在另一个片段中称呼它

@Override
public void postFinished(){
    getFragmentManager().popBackStack();
}

Android Java中有这样的东西吗?

2 个答案:

答案 0 :(得分:3)

您可以在“活动”页面上为BackStackChange添加监听器,

     getSupportFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {

                       // Your Code Here

                }
            });

答案 1 :(得分:0)

you can do in onViewCreated() method:

if (savedInstanceState == null && !mAlreadyLoaded) {
    mAlreadyLoaded = true;

    // Do this code only first time, not after rotation or reuse fragment from backstack
}
Because when android put fragment on backstack, it only destroy its view, but don't kill instance itself, so mAlreadyLoaded will be still true when fragment will be restored from backstack.