2011-10-19 154 views
9

當在片段更改的ActionBar,稱爲活動我顯示這樣的總線線路的列表中:使用片段

Page 1, when opening for the first time.

然後,一旦用戶點擊「站」,我喜歡顯示當然的電臺列表。 我使用這個代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.act_long_distance); 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.f_long_distance, new LongDistanceFragment()).commit(); 
} 

@SuppressWarnings({"UnusedDeclaration"}) 
public void showStationList(View view) { 
    String tag = (String) view.getTag(); 
    if (tag != null && tag.length() > 0) { 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     StationListFragment fragment = new StationListFragment(tag.split(",")); 
     ft.add(R.id.f_long_distance, fragment); 
     // ft.replace(R.id.f_long_distance, fragment); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
     ft.addToBackStack(null); 
     ft.commit(); 
    } 
} 

的XML這項活動是:

<LinearLayout 
    android:id="@+id/ll_container" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
    <FrameLayout 
     android:id="@+id/f_long_distance" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</LinearLayout> 

StationListFragment是一個簡單的ListFragment在另一個的頂部顯示:

After clicking on the Station List button

雖然是ActionBar,但它現在正確地包含標題只要。

什麼不行,如果我現在按站列表是隱藏的,但舊的動作條未恢復:

ActionBar after coming back from List button

該文檔是說添加的動作條的方式是使用onCreateOptionsMenu方法等

所以,在LongDistanceFragment(第一個顯示),我創建這樣的吧:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    Log.d(TAG, "onViewCreated"); 
    super.onViewCreated(view, savedInstanceState); 

    setHasOptionsMenu(true); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    super.onCreateOptionsMenu(menu, inflater); 

    ActionBar bar = getSupportActivity().getSupportActionBar(); 
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 
    bar.setListNavigationCallbacks(new SimpleSpinnerArrayAdapter(getActivity()), this); 
} 

但不知何故它不會恢復,一旦用戶回到該片段。

我想回滾Fragment Transaction時需要恢復ActionBar狀態的一種方法。

我錯過了什麼?謝謝你的幫助。

+0

我現在也在爲此而戰。這是一個相關的問題。 http://stackoverflow.com/questions/6503189/fragments-onresume-from-back-stack –

回答

9

如果我正確理解您的代碼,則問題可能在您的StationListFragment之內。

ActionBar與活動本身相關聯,而不是其中的特定片段。

我猜測在StationListFragment中你正在修改ActionBar來顯示「Stations」標題並禁用List導航模式。在這種情況下,按下後退按鈕將取消事務 - 有效地刪除StationListFragment - 但不會自動撤銷對活動的操作欄所做的任何更改。

您需要做的是覆蓋站列表片段中的一個狀態更改處理程序,以在撤消/隱藏時撤銷ActionBar更改。

一個更好的方法是使用一個replace交易與StationListFragment交換LongDistanceFragment(而不是增加後者對前者的頂部)。然後,您可以添加代碼來配置操作欄的每個片段,使他們當它們變得可見時(通過覆蓋onResume)而不是在清除被清除之後嘗試清理時,將其設置得當。

+2

在ListFragment中沒有任何狀態改變處理程序實際上被調用,所以你沒有辦法做到這一點。這是API中的一個小姐。 –

+1

我正在使用替換,但我將詳細信息視圖添加到後端堆棧。然後當後退按鈕被按下時,列表中沒有事件被觸發,以便能夠重置標題。 –

+0

然後,他在哪裏更改ActionBar標題?根據此示例中的代碼,它不應該顯示「公交線路停靠」 - 我必須假設發生在StationListFragment中。 –

1

我正在處理同樣的問題,當我在第二個Fragment的onStop方法中恢復對ActionBar的更改時,它工作正常。