How do I use the “extend” class to use the “base” PreferenceActivity class in Android?
Here’s the situation: I’m using “BaseClass” with some standard features like a standard menu generator, and all the other activities “extend” it to get the same menu and other stuff :
public class Base extends Activity {
some stuff
}
And then
public class MainActivity extends Base {
some other stuff
}
public class AdditionalActivity extends Base {
some other stuff x2
}
etc.
The problem arises when I want the same functionality in the Preferences activity, when the class has to “extend” the PreferenceActivity instead of the standard activity. How can I handle it better? I’ve read about the “implementation vs. extension” behavior and things like that, but I’m not very experienced with OOP to deal with this and find the best solution. C&P stuff from Base to Prefs classes, which extends PreferenceActivity seems to solve the problem, but it’s certainly the worst solution.
Thank you in advance for your assistance!
Solution
You might want to create a PreferenceBaseActivity
class that extends the PreferencesActivity
. From here, you have a few options:
- Either reimplement the same method in
BaseActivity
(not optimal). - Put methods in BaseActivity into the
ActivityHelper
class, and have bothBaseActivity and PreferencesBaseActivity
with references to helpers.In this way, you can delegate your work from your Base Activity to
ActivityHelper
.