android - setDisplayHomeAsUpEnabled() 在 PreferenceActivity 中不起作用

标签 android android-preferences preferenceactivity android-sharedpreferences

我有一个 SettingsActivity,它扩展了 PreferenceActivity 以显示我的应用中的设置。

有一个后退箭头,但它不起作用。

这是 SettingsActivity.java 文件的代码:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

    private AppCompatDelegate mDelegate;

    public static final String RESET_PASSWORD_KEY = "reset_password";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        SharedPreferences sharedPreferencesCompat = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        Preference myPref = (Preference) findPreference(RESET_PASSWORD_KEY);
        myPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                //open browser or intent here
                Intent resetPasswordActivityIntent = new Intent(SettingsActivity.this, ResetPasswordActivity.class);
                startActivity(resetPasswordActivityIntent);
                return true;
            }
        });

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    public ActionBar getSupportActionBar() {
        return getDelegate().getSupportActionBar();
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
        getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public MenuInflater getMenuInflater() {
        return getDelegate().getMenuInflater();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().setContentView(view, params);
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().addContentView(view, params);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        getDelegate().setTitle(title);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDelegate().onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    public void invalidateOptionsMenu() {
        getDelegate().invalidateOptionsMenu();
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(RESET_PASSWORD_KEY)) {

        }
    }
}

这是 preferences.xml 文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:title="xxx"
        android:summary="xxxxxx"
        android:key="reset_password" />
</PreferenceScreen>

这是在 AndroidManifest.xml 中定义“SettingsActivity”的方式:

<activity
    android:name=".SettingsActivity"
    android:label="@string/title_activity_settings"
    android:parentActivityName=".MainActivity">
</activity>

我怎样才能使那个箭头起作用?

请告诉我。

抱歉,如果问题格式不合适。我还是个初学者。

最佳答案

您需要在 onCreate() 中调用您的私有(private)方法 setSupportActionBar()

此外,您可能想要实现 onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

关于android - setDisplayHomeAsUpEnabled() 在 PreferenceActivity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36433299/

相关文章:

android - Volley Post JsonArrayRequest 给客户端错误,同时在 postman 工作中测试相同的输入

android - 如何使用新的 Android-X API 从当前的 PreferenceFragment 打开一个新的 PreferenceFragment?

android - 如何将共享首选项值设置为布局

android - ActivityNotFoundException(是的,此 Activity 在 AndroidManifest.xml 中声明)

java - Android:将 PreferenceFragment 与单个布局中的其他小部件结合起来

Android GestureDetector onScroll 意外行为

android - 实体中不存在主键中引用的房间

Android Preferenceactivity getView

android - WebRTC 本地信令服务器

java - 将自定义首选项添加到系统设置