工具栏箭头在 NavigationDrawer 的 PreferenceFragment 中不起作用

Toolbar arrow don't work in PreferenceFragment in NavigationDrawer

我正在使用从 NavigationDrawer 启动的 PreferenceFragment。在 preferenceFragment 中,我显示了工具栏。一切看起来都不错,但是当我按工具栏中的箭头返回时它不起作用。我可以在日志中看到:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

但不要什么都不做。返回的唯一方法是按下设备中的返回按钮

我们将不胜感激。

这是我的代码: 偏好片段:

public class AnPreferenceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pref_with_actionbar);
        getFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new SettingsPreference())
                .commit();
    }

    public static class SettingsPreference extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        }
    }
}

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:navigationContentDescription="@string/abc_action_bar_up_description"
        app:navigationIcon="?attr/homeAsUpIndicator"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/action_settings" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appbar" />

</RelativeLayout>

首先在片段的 onCreate() 中声明它具有选项菜单:

setHasOptionsMenu(true);

那么你应该自己处理菜单:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case android.R.id.home:
            // close fragment here
            getActivity().getFragmentManager().popBackStack();
            return true;
        default:
            break;
    }
    return false;
}

如果您需要更多菜单,请创建新菜单项:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.fragment_menu, menu);
    return true;
}

从片段访问工具栏不是一个好的方法,您应该以更好的方式处理它。那里有很多关于这个问题的话题。 (事实上​​ Activity 应该处理工具栏行为)