setDisplayHomeAsUpEnabled() not working in PreferenceActivity
Asked Answered
U

6

20

I have a SettingsActivity which extends PreferenceActivity to show settings in my app.

There is a back arrow, but it is not working.

Here's SettingsActivity.java file's code:

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)) {

        }
    }
}

Here's preferences.xml file's code:

<?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>

Here's how 'SettingsActivity' is defined in AndroidManifest.xml:

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

How can I make that arrow operational?

Please let me know.

Sorry, if question format seems inappropriate. I'm still a beginner.

Uveitis answered 5/4, 2016 at 17:36 Comment(1)
where is your toolbar? – Ravenravening
F
57

You need to call your private method setSupportActionBar() inside onCreate().

Also, you might want to implement onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Folkways answered 5/4, 2016 at 17:43 Comment(0)
S
18

You can do it like this

@Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
Subside answered 12/9, 2020 at 13:10 Comment(1)
I don't know why nobody noticed this answer, but this was the easiest and the best answer that worked for me.. πŸ‘πŸ» – Byzantine
M
3

You forgot to call this method in your onCreate()

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Millionaire answered 5/4, 2016 at 17:40 Comment(0)
R
3

I have made some assumptions on what you might be trying to achieve. This should work if you want to use a custom toolbar with PreferenceActivity. The following code goes inside onPostCreateView()

LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
mToolBar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.toolbar, root,false);
mToolBar.setTitle(R.string.title);
// insert at top
root.addView(mToolBar, 0);

setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Ravenravening answered 5/4, 2016 at 17:43 Comment(1)
where should I add this? – Uveitis
N
0

In Android targetSdkVersion 29

inside onCreate()

getActionBar().setDisplayHomeAsUpEnabled(true);

then implement onOptionsItemSelected from @Matias Elorriaga Answer

Nonrigid answered 8/8, 2020 at 19:36 Comment(0)
G
0

If all else fails, try calling setDisplayHomeAsUpEnabled() twice with different arguments:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Global answered 19/6, 2022 at 13:17 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.