16

Is it possible to use ActionBarActivity in conjunction with PreferenceActivity. Basically, I want to use the following code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Enables the "back" arrow

The issue is PreferenceActivity doesn't contain the getSupportActionBar() method and ActionBarActivity doesn't contain any methods for managing and displaying preferences.

William Seemann
  • 3,440
  • 10
  • 44
  • 78

3 Answers3

7

If you are willing to turn your PreferenceActivity into a PreferenceFragment there is an implementation based on support-v4 Fragment:

https://github.com/kolavar/android-support-v4-preferencefragment

Im using it myself and it works just fine in conjunction to ActionBarActivity!

Ostkontentitan
  • 6,930
  • 5
  • 53
  • 71
  • can you add some code example? I downloaded it, changed the import from android.preference.PreferenceFragment to android.support.v4.preference.PreferenceFragment, and I see it added some headers in the middle of the screen, but not the ActionBar on the top – Gavriel Aug 03 '14 at 10:55
  • It doesnt add the actionbar thats the activity's job. Unfortunally i have no samplecode at hand but it should work similar to: http://developer.android.com/reference/android/preference/PreferenceFragment.html – Ostkontentitan Aug 04 '14 at 06:55
3

ActionBarCompat the feature is not supported yet.

How to add Action Bar from support library into PreferenceActivity?

ActionBar been added in the HoneyComb can use the following code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    getActionBar().setDisplayHomeAsUpEnabled(true);
Community
  • 1
  • 1
HwangTi
  • 31
  • 2
  • 3
    From the newest support library for Android 5.0 this does not work any longer. When you enable Material Themes, there will be no ActionBar and getActionBar() will always return null, thus there will be a NullPointerException. – Zordid Nov 05 '14 at 17:45
  • Exactly, and it's pissing me off ! – Someone Somewhere Sep 03 '15 at 01:44
3

For some people want use getSupportActionBar() with PreferenceActivity. Compiled with Android 6

Declare this in your class extends PreferenceActivity

private AppCompatDelegate mDelegate;

And add this:

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

And call your actionbar:

android.support.v7.app.ActionBar actionBar = getDelegate().getSupportActionBar();