FragmentTransaction不允许添加到后台堆栈?

时间:2011-11-22 17:37:20

标签: android android-fragments illegalstateexception

为什么我的片段无法添加到后台?

@Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction transaction = null;


            ListFragment newListFragment = new bListFragment();
            Fragment newFragment = new EntryFrag();
            transaction = getFragmentManager().beginTransaction();
            ft.replace(R.id.frameOne, newListFragment);
            ft.replace(R.id.frameTwo, newFragment);


        ft.addToBackStack(null);
        transaction.commit();
    }

logcat错误

11-22 12:30:41.370: E/AndroidRuntime(13989): FATAL EXCEPTION: main
11-22 12:30:41.370: E/AndroidRuntime(13989): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.bowling/com.tyczj.bowling.Tabs}: java.lang.IllegalStateException: This FragmentTransaction is not allowed to be added to the back stack.
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread.access$1500(ActivityThread.java:122)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.os.Looper.loop(Looper.java:132)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread.main(ActivityThread.java:4028)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at java.lang.reflect.Method.invoke(Method.java:491)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at dalvik.system.NativeStart.main(Native Method)
11-22 12:30:41.370: E/AndroidRuntime(13989): Caused by: java.lang.IllegalStateException: This FragmentTransaction is not allowed to be added to the back stack.
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.BackStackRecord.addToBackStack(BackStackRecord.java:422)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.tyczj.bowling.Tabs$TabListener.onTabSelected(Tabs.java:155)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:483)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:303)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:883)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at com.tyczj.bowling.Tabs.onCreate(Tabs.java:32)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-22 12:30:41.370: E/AndroidRuntime(13989):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)

4 个答案:

答案 0 :(得分:10)

您正在使用操作栏选项卡,并且您希望将绑定到选项卡的片段添加到后台堆栈,通常是禁止的(在大多数情况下这是可以理解的)。操作栏选项卡不允许添加到后台堆栈。如果您的应用程序是基于选项卡的,那就没问题,但是如果您只在特定情况下显示选项卡,而在其他情况下您隐藏它们,这种行为并不好(我不明白Google为什么不放弃它).....也许你需要将第一个选项卡添加到后台堆栈(我的情况),但在这种情况下onTabSelected将不允许您将事务添加到后台堆栈,它将抛出异常,因为addToBackStack在此方法调用中被阻止(在FragmentTransaction中有一个方法disallowAddToBackStack正是这样做的......我完全不同意这个约束。)

答案 1 :(得分:2)

我不明白为什么要在函数中实例化一个新的FragmentTransaction,只需使用作为参数传递的那个。我没有对此进行测试,但我非常确定它应该可行。

@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        ListFragment newListFragment = new bListFragment();
        Fragment newFragment = new EntryFrag();
        ft.replace(R.id.frameOne, newListFragment);
        ft.replace(R.id.frameTwo, newFragment);
        ft.addToBackStack(null);
        ft.commit();
}

答案 2 :(得分:0)

删除transaction,然后使用回调中提供的FragmentTransaction。当一个人已经交给你时,没有必要实例化一个新的FragmentTransaction。将任意内容传入ft.addToBackstack();,然后拨打ft.commit();

答案 3 :(得分:0)

您的代码中唯一的问题是您“正确”创建了一个事务,但之后您永远不会使用它。提供的事务FragmentTransaction ft无法添加到backstack,因为侦听器不支持它,但这应该有效:

@Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {

            ListFragment newListFragment = new ListFragment();
            Fragment newFragment = new EntryFrag();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frameOne, newListFragment);
            transaction.replace(R.id.frameTwo, newFragment);


            transaction.addToBackStack(null);
            transaction.commit();
    }

请注意transaction.replace(... ft.replace(... commit()的使用,您必须自己FragmentTransaction ft这样做,因为TabListener只会自动提交{{1}}

此外,您可以在此处阅读有关添加到TabListener API文档中不支持的后台堆栈的内容:http://developer.android.com/reference/android/app/ActionBar.TabListener.html