在片段

时间:2018-02-08 16:59:38

标签: android android-fragments fragment-backstack fragmentmanager

我在活动中有一个包含片段和两个简单​​方法的活动,用于管理片段:pushFragmentImmediate()popFragmentImmediate()

public void pushFragmentImmediate(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, fragment)
            .addToBackStack(null)
            .commitNow();
}

public void popFragmentImmediate() {
    getSupportFragmentManager().popBackStackImmediate();
}

这些事务必须立即执行,因为遗留代码依赖于后备堆栈始终是最新的。

现在我有一个Fragment,如果满足某些条件,会在onResume期间弹出:

@Override
public void onResume() {
    super.onResume();
    if (condition) {
        ((Main) getActivity()).popFragment();
    }
}

这会出错。 如果不将popStackImmediate()更改为popBackStack(),是否可以避免此错误?或者,当它不会返回错误时,我可以将popBackStackImmediate()移动到其他阶段吗?或者我是以错误的方式思考这个问题?

完整追踪:

java.lang.RuntimeException: Unable to resume activity {se.esillen.testproject/se.esillen.testproject.Main}: java.lang.IllegalStateException: FragmentManager is already executing transactions
                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3433)
                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3473)
                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736)
                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1481)
                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                           at android.os.Looper.loop(Looper.java:179)
                                                           at android.app.ActivityThread.main(ActivityThread.java:6152)
                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                        Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions
                                                           at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2167)
                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2223)
                                                           at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:834)
                                                           at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:794)
                                                           at se.esillen.testproject.Main.popFragment(Main.java:25)
                                                           at se.esillen.testproject.TestFragment.onResume(TestFragment.java:26)
                                                           at android.support.v4.app.Fragment.performResume(Fragment.java:2401)
                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1465)
                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                           at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)
                                                           at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3181)
                                                           at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214)
                                                           at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:470)
                                                           at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:459)
                                                           at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
                                                           at android.app.Activity.performResume(Activity.java:6856)
                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3410)
                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3473) 
                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736) 
                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1481) 
                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                           at android.os.Looper.loop(Looper.java:179) 
                                                           at android.app.ActivityThread.main(ActivityThread.java:6152) 
                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

 

0 个答案:

没有答案