2011-11-22 86 views
2

爲什麼不能將我的碎片添加到背堆棧?FragmentTransaction不允許添加到回棧中?

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

回答

0

擺脫你transaction的,相反,用在回調提供的FragmentTransaction。當一個人已經交給你時,沒有必要實例化一個新的FragmentTransaction。傳入ft.addToBackstack();,然後致電ft.commit();

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

您使用操作欄選項卡,你想綁定到標籤的片段被添加到後退堆棧,通常是被禁止(這是可以理解的內心深處的情況下)。操作欄選項卡不允許添加到Back Stack。如果你的應用程序是基於標籤的,那就沒問題,但是如果你只在特定情況下顯示標籤,而在其他情況下則隱藏它們,這種行爲並不好(我不明白Google爲什麼不讓它).....也許你需要將第一個選項卡添加到後退堆棧中(我的情況),但在這種情況下,onTabSelected將不允許您將事務添加到後退堆棧,它將拋出異常,因爲addToBackStack在此方法調用中被阻止(在FragmentTransaction中有一個方法disallowAddToBackStack,它完全不符合這個約束)。

+4

聖密牆,蝙蝠俠!你可以編輯一些段落嗎? –

+1

你將如何改進代碼 – toxicate20

0

你的代碼唯一的問題是你'正確'創建一個事務,但是你永遠不會使用它。所提供的交易FragmentTransaction ft不能被添加到堆棧中,因爲它不是由聽衆的支持,但是這應該工作:

@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()這個自己,一旦你正確地做,因爲TabListener只會自動提交FragmentTransaction ft

此外,您還可以讀到增加在這裏的TabListener API文檔不被支持的背堆棧:文本http://developer.android.com/reference/android/app/ActionBar.TabListener.html