0

I've got a class (it's my main activity) that already extends other class:

public class AdvancePreferences extends CustomPreferences 

I would like to use v7 ActionBar. According to this site: https://developer.android.com/training/basics/actionbar/setting-up.html I need to extend ActionBarActivity. It looks like I need multiple inheritance here:

public class AdvancePreferences extends CustomPreferences extends ActionBarActivity

How can I resolve this problem? I do not want to lose Preferences feature.

-------------- more details: I am not sure if this matter, but: My AdvancePreferences extends CustomPreferences that is custom class. CustomPreferences extends PreferenceActivity and implements OnSharedPreferenceChangeListener. Now I need AdvancePreferences to extend not only CustomPreferences but ActionBarActivity also.

-----------------important: I use minSdkVer = 7

Malvinka
  • 1,185
  • 1
  • 15
  • 36
  • Edit: Didn't realize Preferences was the Android one, I thought it was a custom class. Silly me. But I did find some code related to this for you: https://code.google.com/p/actionbarcompat/source/browse/src/org/mariotaku/actionbarcompat/app/ActionBarPreferenceActivity.java?r=1a2c145b61b34724bb91b5ae71e4f8cec1aba274 – Cruceo Apr 03 '15 at 17:28
  • Ahh.. well.. it is custom indeed... silly ME :D I edited my question so now there should be no doubts. Sorry. – Malvinka Apr 03 '15 at 19:21
  • What is CustomPreferences? Is it a PreferenceActivity? – Emanuel Moecklin Apr 03 '15 at 19:30
  • It extends PreferenceActivity and implements OnSharedPreferenceChangeListener – Malvinka Apr 03 '15 at 19:33
  • **1)** Since you probably want that material look, you ned the to extend `ActionBarActivity`. **2)** You can then use native `PreferenceFragment` via `getFragmentManager()` but only if your minSdk is higher than or equal to 11. If your minSdk is lower use a preference fragment backport such as [this one](https://github.com/Machinarius/PreferenceFragment-Compat) via `getSupportFragmentManager()`. – Eugen Pechanec Apr 03 '15 at 20:00

2 Answers2

1

Java does not allow multiple inheritance.

By the way, you could make it extends first from ActionBarActivity (Maybe you could use this class for more Activities that use the ActionBar), and then extends that class again to use the Preferences.

With this approach, the inheritance tree will be: public class BaseActionBarActivity extends ActionBarActivity (This could be used for more activities) public class PreferencesActivity extends BaseActionBarActivity (this one is fully focused on whatever Preferences do)

On the other hand, if you want to use the PreferencesActivity of Android approach, you could use Fragments to handle this, but I don't know exactly what are you doing and what Preferences class do.

By the way, look this links for more information (In the case you need the use of PreferencesFragment/Activity) : PreferenceFragment android and PreferenceActivity

SekthDroid
  • 140
  • 1
  • 9
  • Thanks a lot. My Preference class is custom class. I am sorry for understatement. I am not sure how to implement your approach with "inheritance tree". If I will implement new class BaseActionBarActivity extends ActionBarActivity, and then I will make my AdvancePreference class extends BaseActionBarActivity where is my CustomPreference class then? Sorry, I am beginner... – Malvinka Apr 03 '15 at 19:25
1

As already mentioned in SekthDroid's answer you can't inherit from two classes. The way to go here is to extend ActionBarActivity and instead of using the PreferenceActivity put your preference related logic into a fragment that extends PreferenceFragment.

This is the recommended way to implement this unless you're still developing for pre-Honeycomb devices:

Prior to HONEYCOMB this class [PreferenceActivity] only allowed the display of a single set of preference; this functionality should now be found in the new PreferenceFragment class. If you are using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.

http://developer.android.com/reference/android/preference/PreferenceActivity.html

PreferenceFragments are very similar to use and it should be straight forward to move your logic from your CustomPreferences activity class to a CustomPreferences fragment class.

So the whole hierarchy would look like this:

  • public class AdvancePreferencesActivity extends ActionBarActivity
  • public class CustomPreferencesFragment extends PreferenceFragment
  • public class AdvancePreferencesFragment extends CustomPreferencesFragment

All the extra preference logic you currently have in AdvancePreferences would go into AdvancePreferencesFragment, while AdvancePreferencesActivity is merely a bare-bone Activity that adds AdvancePreferencesFragment as a fragment:

http://developer.android.com/training/basics/fragments/creating.html

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • Thanks a lot. Looks a bit complicated but I'll try to implement this :) – Malvinka Apr 03 '15 at 19:51
  • It's really not, it's just moving code around without adding much :-), but I understand if that sounds overwhelming in the beginning. – Emanuel Moecklin Apr 03 '15 at 20:31
  • Ok. What if I DO develop for older devices? I do not understand what that: "If you are using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here." does mean. I have a problem with PreferenceFragment class, as it generates error about higher required API. – Malvinka Apr 06 '15 at 16:17
  • The quote means it's discouraged to use PreferenceActivity, use PreferenceFragment instead. The question is not "What if I DO develop for older devices" but "Do you really develop for pre-Honeycomb = Android 2.x devices"? Do you? – Emanuel Moecklin Apr 06 '15 at 16:27
  • I do. I use appcompat v7, and min SdkVersion is 7. – Malvinka Apr 06 '15 at 16:40
  • I did some research. Seems like it's not trivial problem anymore. Ah... good thing I had an opportunity for some "android fragments exercise" ;) – Malvinka Apr 06 '15 at 16:52
  • Emanuel Moecklin: "The "right" approach would be to use fragments on Honeycomb and higher and PreferenceActivity on previous versions." As I am going to delete answer this comment refers to I wanted to copy that, as very useful. Thank you a lot. Can I "integrate" ActionBar and PreferenceActivity on older sdk version? – Malvinka Apr 06 '15 at 19:35