FragmentTransaction is not allowed to be added to back stack?

喜你入骨 提交于 2019-12-02 05:24:48

问题


why cant my fragments be added to back stack?

@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 error

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)

回答1:


Get rid of your transaction and, instead, use the FragmentTransaction provided in the callback. There's no need to instantiate a new FragmentTransaction when one is already handed to you. Pass in whatever you want into your call to ft.addToBackstack(); and then call ft.commit();




回答2:


You are using Action Bar tabs and you want the Fragments bound to the tabs to be added to the back stack, usually that is forbidden (and it is understandable inmost cases). Action Bar Tabs are not allowed to be added to the Back Stack. If your application is tab based it is ok, but if you display tabs only in particular situations and in others you hide them this behavior is not good (and I do not understand why Google does not let it).....maybe you need the first tab to be added to the back stack (my situation), but in such a case onTabSelected will not allow you to add the transaction to the back stack, it will throw an exception because the addToBackStack is blocked inside this method call (there is the method disallowAddToBackStack inside FragmentTransaction that does exactly that...I totally disagree with that constraint).




回答3:


Im not getting why you are instantiating a new FragmentTransaction inside your function, just use the one passed as a parameter. I didnt test this, but im pretty sure that it should work.

@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();
}



回答4:


The only problem in your code is that you 'correctly' create a transaction, but then you never use it. The supplied transaction FragmentTransaction ft cannot be added to the backstack because it is not supported by the listener, however this should work:

@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();
    }

Note the use of transaction.replace(... vice ft.replace(... also you must commit() this yourself, as you properly did, because TabListener will only auto-commit FragmentTransaction ft

Also, you can read about adding to the back stack not being supported in the TabListener API docs here: http://developer.android.com/reference/android/app/ActionBar.TabListener.html



来源:https://stackoverflow.com/questions/8231161/fragmenttransaction-is-not-allowed-to-be-added-to-back-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!