views:

1653

answers:

2

Hello everyone,

I've been learning to develop in Android and had more of a general question: If I have a layout that uses a list and some other views, should I be using Activity or ListActivity for my activity class?

I know that ListActivity will probably make things easier to override list-specific events, but is there any other advantage to using a ListActivity over Activity? What if I wanted to change the layout in the future to a GridView? Would it be any more/less of a pain to change the class' code?

I was just curious about "best practices" in this regard and what the benefits entail, so any answer would be helpful :)

Bara

A: 

I'd go with Activity. Don't really see the reason to use ListActivity, unless you want to do something trivial and you know it's gonna be with the List.

Alex Volovoy
Does that mean there are no advantages to using ListActivity over Activity and it's just there as a "shortcut" of sorts? Is there some kind of standard for this that Google expects?
Bara
To me advantages are as Dave stated above, saving some time and make code little bit cleaner. Look at the ListActivity in docs or in the source code as Dave suggested and see if you need that convenience. I personally, use activity in the most cases.
Alex Volovoy
I agree. ListActivity has just been not enough. It can still be nice to something trivial.
Fabio Milheiro
+10  A: 

I'd use a ListActivity since it gives you a lot of shortcut methods to make things easier and keep your code more readable.

For example, you get the onListItemClick() method which is called whenever you click a item which saves you from creating a separate listener.

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 and see that it doesn't do all that much other than make your life a little easier.

Dave Webb