Fragment.isAdded returns true after fragment removed from a container

后端 未结 2 425
情书的邮戳
情书的邮戳 2021-01-02 05:33

I have an activity with below layout



        
2条回答
  •  一个人的身影
    2021-01-02 06:36

    maybe your can encapture commition of FragmentTransaction like this

    private void commitFragmentTransaction(final FragmentTransaction ft, boolean allowStateLoss, boolean now) {
        if (ft == null || ft.isEmpty()) {
            return;
        }
    
        if (allowStateLoss) {
            if (now) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    ft.commitNowAllowingStateLoss();
                } else {
                    ft.commitAllowingStateLoss();
                    mFragmentManager.executePendingTransactions();
                }
            } else {
                ft.commitAllowingStateLoss();
            }
        } else {
            if (now) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    ft.commitNow();
                } else {
                    ft.commit();
                    mFragmentManager.executePendingTransactions();
                }
            } else {
                ft.commit();
            }
        }
    }
    

    commitNow() and commitNowAllowingStateLoss() is Added in API level 24

    Calling commitNow is preferable to calling commit() followed by executePendingTransactions() as the latter will have the side effect of attempting to commit all currently pending transactions whether that is the desired behavior or not.

提交回复
热议问题