質問

このようなバスラインのリストを表示しているアクティビティ内で呼ばれるフラグメントで:

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は復元されていません。

ActionBar after coming back from List button

ドキュメントは、ActionBarを追加する方法は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);
}

しかし、どういうわけか、ユーザーがそのフラグメントに戻っても復元されません。

フラグメントトランザクションをロールバックするときにアクションバーを回復する方法が必要だと思います。

何が足りないの?助けてくれてありがとう。

役に立ちましたか?

解決

If I understand your code correctly, the problem is likely within your StationListFragment.

The ActionBar is associated with the Activity itself, not a particular Fragment within it.

I'm guessing that within the StationListFragment you're modifying the ActionBar to display the "Stations" title and disable the List navigation mode. In that case, pressing the back button undoes the transaction -- effectively removing the StationListFragment -- but doesn't automagically undo any changes you might have made to the Activity's Action Bar.

What you'll need to do is override one of the state change handlers within your Station List Fragment to undo the ActionBar changes when it's removed / hidden.

A better approach would be to use a replace transaction to exchange the LongDistanceFragment with the StationListFragment (rather than adding the latter on top of the former.) Then you can add the code to configure the Action Bar to each Fragment so they set it up appropriately when they become visible (by overriding onResume) rather than trying to clean up after one's been removed.

他のヒント

私は同じ問題に対処していましたが、変更をactionbarに戻すと onStop 2番目のフラグメントの方法、正常に動作します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top