Android工具栏如何保存状态?

时间:2016-06-06 13:37:06

标签: android android-support-library androiddesignsupport

保存android.support.v7.widget.Toolbar状态的正确方法是什么?

我原以为它应该保存在开箱即用之外。 Ids在xml中设置,但仍然不起作用。

我已经尝试了下面的东西,但它不起作用。

我是否必须通过getter和setter手动保存所有内容?或者我会错过什么?

似乎是错误。

活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initializeInjector();
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);


    if (savedInstanceState != null) {
        tabFragment = (TabFragment) getSupportFragmentManager().findFragmentByTag(TAB_FRAGMENT_TAG);
    } else {
        tabFragment = getTabFragmentInstance();
        getSupportFragmentManager().beginTransaction().add(R.id.containerView,tabFragment , TAB_FRAGMENT_TAG).commit();
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    ParcelableSparseArray toolbarState = new ParcelableSparseArray();
    toolbar.setSaveEnabled(true);
    toolbar.setSaveFromParentEnabled(true);
    toolbar.saveHierarchyState(toolbarState);
    outState.putSparseParcelableArray(TOOLBAR_STATE,toolbarState);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    SparseArray<Parcelable> toolbarState = savedInstanceState.getSparseParcelableArray(TOOLBAR_STATE);
    toolbar.restoreHierarchyState(toolbarState);
}

布局文件:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/activity_main_Layout_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </FrameLayout>


    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:saveEnabled="true"
            app:layout_scrollFlags="enterAlways"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"

            />


    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:itemTextColor="@color/black"
    app:menu="@menu/drawer_menu"
    />

THX

Edit01:我正在设置工具栏的属性,即。可见性,标题和菜单。旋转后,这些似乎从.xml中膨胀。工具栏由片段设置并从活动中注入。

0 个答案:

没有答案