11

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?

BamsBamx
  • 4,139
  • 4
  • 38
  • 63

4 Answers4

11

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.

BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • OOoh. Multiple answers all saying the same thing! Give it to the underdog! :) – BrantApps Aug 15 '12 at 12:55
  • Essentially they are the same thing but the ListActivity has some useful helper methods that have probably been implemented efficiently and well by the Android team thus are performing to the best of their abilities. The far more pertinent item with performance and lists is managing your cursor and not doing silly things like retrieving all the items in a list only to use the first 10 etc. – BrantApps Aug 15 '12 at 13:25
3

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.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
2

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

Andy Res
  • 15,963
  • 5
  • 60
  • 96
0

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
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118