While programming on Android, I end up writing a parent activity which is extended by several others. A bit like ListActivity. My parent activity extends Activity. if I intend to use a Map or a List, I can't use my parent activity as superclass - the child activity can only extend one activity obviously. As such I end up writing my parent activities with the same logic for Activity, ListActivity, MapActivity and so forth.
What am I looking for is some sort of trait functionality/design pattern which would help in this case. Any suggestions?
I really dislike ListActivity, MapActivity etc. Basically they are activities which simply add a single view element at the cost of some flexibility. By adding a MapView or ListView to your XML appropriately, you end up with the same thing which can extend an Activity derived superclass directly. So just don't use any of those SomethingActivity classes for the most part.
I've ended up having a base MyAbstractActivity extends Activity that incorporates shared logic and a MyAbstractListActivity extends MyAbstractActivity that mimics ListActivity (inflates layout.R.id.list, layout.R.id.empty, etc.; not much going on there).
I'm using a delegate with all the shared functionality. This enables me to have all the shared functionality in one single class for all the different activities.
All my activities extend their special activity and then implement a common interface. The problem with this approach is that I need to implement all the methods defined in the interface and call the matching method in the delegate object. This code at the moment amounts to 30 duplicate lines of code and I think that is not that much of a problem.
Related
I am extending ListActivity in one of my Activities but I need to extend one more Activity.
So, is there an alternative for extending ListView in Android?
I am using the ListView to display the data from database.
First of all: Java does not support multiple inheritance.
After this, what I would to, is to rethink in the design: does it make sense to your class to be ListActivity and other Activity?
If yes, there are a couple of workarounds you can try:
Aggregation: make a class that takes those two activities as fields.
Interfaces.
I'm designing a similar interface to MathStep pictured below but my main activity already extends
public class MainActivity extends Activity implements TextWatcher
and Java doesn't have multiple inheritance.
In this program, you can ViewPager between Basic, functions and extra tabs. My program is not using tabs but I am swiping between fragments of buttons in a RelativeView.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
I have been working on this with a friend for, I'm not kidding, 8 hours before we resorted to asking a question here. I have looked at this and this post and they were very helpful in understanding how ViewPager and Fragments work, but in all the examples they reference full paged ViewPagers, every time we attempted to create this scenario, something either wouldn't compile, or we weren't linking our XML correctly and code wasn't running... I've searched extensively for this answer and I hope I'm not the only one who has struggled with this so others can learn.
I'll admit, part of the confusion has been the learning curve but that's why I'm doing this, to learn.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
All Fragments have a reference to the Activity via the getActivity() method, but you should only use it if you really need a handle to the context.
Inheritance is not required in any way whatsoever (technicality: other than for your activity, fragment, and FragmentPagerAdapter which must inherit from their respective parent classes...). The ViewPager itself can be included in the view heirarchy by referencing it from XML. The different fragments are displayed in the ViewPager by a FragmentPagerAdapter that you will have to implement yourself, this should be a separate class. If you want, it can be an inner static class, but, do not use an inner class. Keeping the scope organized by forcing dependencies to be passed through constructors will keep your code clean.
You should start by reading the ViewPager/Fragment related documentation on d.android.com. There is example code for these things and once you understand them individually everything will come together.
sorry for may be stupid question, I am a beginner. what can I do if I needed to extend both ListActivity and FragmentActivity in one activity?
I know that I can extend only 1 class, but is there any trick which resolves this?
Thank in advance.
you don't have to extended list activity in order to use list , you can include list View inside your layout file
No, you cannot extend 2 classes in Java. Could you specify why you need to extend both? My guess is FragmentActivity should suffice for your case.
You cannot extend more than one class. You said you're beginner, whenever you dive deeper on how the objects work you'll see that it's simple impossible.
But, to answer the bigger picture of what you're probably trying to achieve:
You want to have one Class that will extend FragmentActivity, and a different class that will extend ListFragment. And whenever the FragmentActivity is created you'll instantiate one of your ListFragments and add it to the layout.
I am using a class which extends Activity to obtain a list from an API...
In some apps which do basically the same, the devs extended ListActivity...
Which are the differences?
ListActivity extends the functionality of the common android.app.Activity by providing a number of list centric features 'for-free' if you like. For example, the handling of a list entry click is neatly contained within ListActivity's onListItemClick(...) whereas if you were using a plain android.app.Activity then you would need to implement this manually with an OnClickListener and implementation.
By all accounts, if your layout contains a list then use a ListActivity/ListFragment since it is a useful extension. It does not mean your whole screen layout has to be list but a portion of it has to host a ListView widget with identifier, id="#android:id/list".
The Javadoc on the class with useful examples of how to use it can be found here.
ListActivities are specially designed to be used with ListViews. It provides several helper methods like onListItemClick(), which make it easier to use a ListView in them.
You can do anything you can do in an Activity in a ListActivity.
If you want to change the layout of a ListActivity you still can with setContentView() method from Activity. As long as there is a ListView called #android:id/list somewhere in your View the ListActivity will still work.
If you're still not sure, you could always look at the source code for ListActivity (Jelly Bean code linked to) and see that it doesn't do all that much other than make your life a little easier.
Extending from the ListActivity you agree with the contract that in the layout of your activity a ListView component will be available.
Your ListView component should have the id: #android:id/list
The ListView class provides convenient methods for working and manipulating the ListView
Also, in a regular Activity you can use the code below in onCreate to hide the app titlebar. It seems that you can't do the same in a ListActivity. (learned this the hard way)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_list);
// The rest of the content of onCreate
In my app I have common methods and attributes that I would like available to both List and regular activities. I want to use inheritance to create a parent class to extend to both types of activities. If I make the parent class extend list activity the regular activity will give me the exception:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Vica versa, I will not have the list functionality I need classes that extends this parent class.
How do I accomplish this without copying the same methods to two different classes that extend listactivity and activity?
Thanks!
No need to use ListActivity unless you're looking for the simplest way to use a ListView.
The way I tackle this is chart out what activities I need, and what functionality I need across all of them, and only some, and extend accordingly.
For example, you can have you super-base activity, BaseActivity. This can have setup, teardown, and whatever other functionality you need for all your child activities. From there, you can have something like BaseListActivity that extends BaseActivity, handles everything you may need from BaseActivity and just add functionality for ListViews.
Activity > BaseActivity > BaseListActivity > MyListActivity
Activity > BaseActivity > MyPlainActivity
Here's a video of some classes I've been working on for managing Cursors as well as ListViews.
ManagedActivity classes - YouTube
You are not obliged to have a ListActivity in order to have a ListView as a View inside your layout.
Based on this, you can make a BaseActivity that extends Activity, and add there all your methods and attributes.
Then all your Activities can extend this BaseActivity.
Hope this helps!