简体   繁体   English

所有活动都可以使用BaseActivity吗?

[英]A BaseActivity for all activities, possible?

There are methods and attributes I would like to share across activities. 我想在各种活动中分享一些方法和属性。 Eg 例如

public class BaseActivity extends Activity

would be the parent for another activity 将是另一项活动的父母

public class MainActivity extends BaseActivity

BUT if the child activity is a eg ListActivity this is not possible, right? 但是如果子活动是一个例如ListActivity,这是不可能的,对吗? Do I need a base class for ListActivity, too? 我也需要ListActivity的基类吗? This would be redundant code. 这将是冗余代码。

I could transform the ListActivity to an Activity, but this would be more code then necessary. 我可以将ListActivity转换为Activity,但这将需要更多代码。

Any suggestions? 有什么建议么?

Yes, you're right. 你是对的。 As java does not support multiple inheritance, a class can only have one base class. 由于Java不支持多重继承,因此一个类只能具有一个基类。

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. 但是您可以通过委托而不是继承来工作:将方法放入单独的类(该类不继承Activity ),并在活动中使用它的实例。 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 http://sourcemaking.com/refactoring/replace-inheritance-with-delegation

How do I implement multiple inheritence in Java 如何在Java中实现多重继承

For the ListActivity case I would create a BaseListActivity that extends from BaseActivity . 对于ListActivity情况,我将创建一个从BaseActivity扩展的BaseListActivity 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 . BaseListActivity将包含一个getListView() ,如果您确实需要它,它将返回ListView ;一个受保护的方法setAdapter() ,它接收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. 您还可以创建一些受保护的方法来启用显示空视图,启用此行为并返回空视图和ListView的ID。 By default these should point to android.R.id.list / android.R.id.empty . 默认情况下,它们应指向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. 最后,这是面向对象的问题,而不是特定于Android的问题。 If you check the implementation of ListActivity you'll see how simple it is to make your own BaseListActivity implementation. 如果您检查ListActivity的实现,您会发现制作自己的BaseListActivity实现非常简单。 In the end it's more important to have a consistent and robust class hierarchy. 最后,拥有一致且健壮的类层次结构更为重要。

I am using my Activities this way, and I'm really happy I'm doing it. 我以这种方式使用我的Activities ,我很高兴自己能做到。 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. 当然,您必须进行一些更改(例如您在问题中提到的ListActivity ),但是在我看来,它的优点是缺点。

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 BroadcastReceiver s 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. 在我的应用程序中,我不必担心每次创建新的Activity都会发送Analytics数据的BaseActivity ,也不必担心在每个类中设置所需的BroadcastReceiver ,我只是将BaseActivity做了抽象,我的BaseActivity每次广播时都调用的抽象函数。

如果您想共享某些方法来实现接口或为baseactivty创建抽象类的最佳方法。

As others have pointed out, you need to use composition, since multiple inheritance is not possible in Java. 正如其他人指出的那样,您需要使用组合,因为在Java中不可能进行多重继承。 This answer to a related question explains how to do this in the case of Android Activities. 这个对相关问题的答案说明了在Android Activities中如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM