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

3

30

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

Ine answered 9/1, 2010 at 5:8 Comment(0)
C
43

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.

Cipher answered 9/1, 2010 at 6:35 Comment(2)
I just went with this approach while trying to refactor some code that uses the same base but both Activity and ListActivity. Placed an empty ListView in the common part of my layout and then shared that common part using <include> and <merge> tags.Perspire
ListActivity has some bugs which manifest themselves in certain use case. So ListActivity is conditonally usable on very very basic use case. And in that case is simple enough to use Activity and ListView.Marylynnmarylynne
S
11

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.

Shebat answered 9/1, 2010 at 5:15 Comment(3)
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?Ine
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.Shebat
I agree. ListActivity has just been not enough. It can still be nice to something trivial.Maestro
C
0

You should use Activity rather than ListActivity because ListActivity codes are non-reusable and you need to write them again and again.

Carnarvon answered 25/1, 2018 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.