1

There are methods and attributes I would like to share across activities. E.g.

public class BaseActivity extends Activity

would be the parent for another activity

public class MainActivity extends BaseActivity

BUT if the child activity is a e.g. ListActivity this is not possible, right? Do I need a base class for ListActivity, too? This would be redundant code.

I could transform the ListActivity to an Activity, but this would be more code then necessary.

Any suggestions?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • what do you mean with redundant? – Blackbelt Nov 18 '13 at 15:47
  • @blackbelt I share the same methods for activity and list activity but for list activity I would need another base class with the same methods as the other base class and this would lead to redundancy – DarkLeafyGreen Nov 18 '13 at 16:03

5 Answers5

5

Yes, you're right. As java does not support multiple inheritance, a class can only have one base class.

But you can work via delegation instead of inheritance: Put your methods into a separate class (which does NOT inherit Activity) and use instances of it in your activities. Then you can reduce the redundant code (creating and holding the instance) to a minimum.

See also:

http://sourcemaking.com/refactoring/replace-inheritance-with-delegation

How do I implement multiple inheritence in Java

Community
  • 1
  • 1
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
4

For the ListActivity case I would create a BaseListActivity that extends from BaseActivity. The BaseListActivity would contain a getListView() that returns the ListView if you really-really need it, a protected method setAdapter() that receives an Adapter/ListAdapter. You could also create some protected methods for enabling to show the empty view, to enable this behavior and to return the ids for your empty view and ListView. By default these should point to android.R.id.list/android.R.id.empty. But you can override that in your child activity.
In the end it's a matter of OOP and not that much of Android specific. If you check the implementation of ListActivity you'll see how simple it is to make your own BaseListActivity implementation. In the end it's more important to have a consistent and robust class hierarchy.

gunar
  • 14,660
  • 7
  • 56
  • 87
1

I am using my Activities this way, and I'm really happy I'm doing it. Of course you have to make some changes (like the ListActivity you mentioned in your question), but it has more advantages then disadvantages in my opinion.

In my app I don't have to worry about sending Analytics data every time I create a new Activity, and I also don't have to worry about setting up BroadcastReceivers I need in every class, I just made my BaseActivity abstract and have an abstract function what I call every time my BaseActivity gets a broadcast.

pshegger
  • 2,526
  • 24
  • 29
0

if you want to share some methods the best way it to impelement an interface or make an abstract class for baseactivty.

cesztoszule
  • 286
  • 3
  • 12
0

As others have pointed out, you need to use composition, since multiple inheritance is not possible in Java. This answer to a related question explains how to do this in the case of Android Activities.

Community
  • 1
  • 1
Florian von Stosch
  • 1,700
  • 1
  • 14
  • 22