Michael
Michael

Reputation: 3229

Do all Android classes extend Activity?

I'm going through some Android development tutorials, and I notice that some classes extend Activity, and others extend ListActivity for example.

By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?

Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?

I read a few similar questions but it wasn't quite clear.

Upvotes: 0

Views: 1730

Answers (4)

Ani
Ani

Reputation: 1061

In Android, All the classes are not extended by Activity.

Only the screens which render the UI are extended by Activity. So, if you want to create a UI screen then you have to explicitly extend it with Activity class and override the callback methods.

ListActivity is subclass of Activity which is only used when you have only a ListView in the your layout.It has some specific listview related methods.

Similarly, if you wanna create a fragment, your class will extend the Fragment Class and override its methods. Similarly for Service, BroadcastReceiver etc.

Upvotes: 1

Andrew Krause
Andrew Krause

Reputation: 128

Newly created classes do not extend anything. ListActivity is a subclass of Activity so all of the functionality Activity has, ListActivity inherits. In the documentation for ListActivity you can see that it is a subclass of Activity.

Upvotes: 1

Razgriz
Razgriz

Reputation: 7343

By "default", all Android Activities extend Activity.

By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?

You have to explicitly state that it should extend Activity if it does not already and if you plan to use it as an Activity. Kindly note that you there are times you wouldn't do it. For example, when you have a class for your objects or whatnot, then you don't need to extend Activity any longer.

Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?

ListActivity, AppCompatActivity, and any other class that ends in -Activity are "children of the Activity class. So by extension, extending by these "children" would already extend from the parent Activity class and you'll no longer need to do something like YourActivity extends ListActivity, Activity. I think Android Studio will give you an error if you do.

The difference between an Activity and a ListAcivity is that a ListActivity will require you to override several methods because it is expecting a ListView in its content.

Upvotes: 2

Harsh Pandey
Harsh Pandey

Reputation: 831

Android is written in Java, and like all java classes, every class inherits from the Object class.

Does a newly created class implicitly extend Activity or do you need to explicitly extend it?

You need to extend it.

Does ListActivity already extend Activity?

Yes.

You can see class hierarchy for ListActivity here.

Upvotes: 2

Related Questions