开发者

Share Same code in Activity and ListActivity

开发者 https://www.devze.com 2023-02-22 04:47 出处:网络
My 开发者_Go百科project consists of couple of activity and ListActivity items, there is some common piece of code(Navigation bar and some other codes) which needs to be done on both type of activity.

My 开发者_Go百科project consists of couple of activity and ListActivity items, there is some common piece of code(Navigation bar and some other codes) which needs to be done on both type of activity.

Is there a way I extend the activity and write my piece of code, and let ListActivity also inherent that code ?

right now I am copying the same piece of code in two classes , one is Activity extended and other is ListActivity extended


You could also have the common code in a class CSuperCommon, and have each of your Activities contain an inner class that inherits from CSuperCommon. Some initialization will need to be done such as setting the parent view, context, etc.


There is no real multi inheritance in java (and so in android) but it is possible to simulate it: http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html.

Here is another answer from stackoverflow: How do Java Interfaces simulate multiple inheritance?


Have both extend a Base Activity class (which will have your common code) and implement a list view in one of them. Implementing a list view is very easy!

0

精彩评论

暂无评论...
验证码 换一张
取 消