Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Activity vs. ListActivity - Which one should my activity class extend?

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

like image 488
Bara Avatar asked Jan 09 '10 05:01

Bara


1 Answers

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.

like image 110
Dave Webb Avatar answered Sep 25 '22 11:09

Dave Webb