2011-10-19 109 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的更改时,它工作正常。