I am having trouble applying the concept of inheritance to Android Activities - eg.
ActivityA extends Activity
and
ActivityB extends ActivityA
, then if I launch with ActivityB then onCreate() method for both Activities (A then B) is called .
My question is, in typical Java , onCreate from ActvityA should be over-ridden - but onCreate is rather behaving as a constructor even though its a function. How does inheritance work in Android, esp. wrt other functions like onPause(),onResume etc. ?
EDIT: I also noticed that ActivityA may have many abstract methods called in its onCreate() whose definitions are provided in ActivityB. How does this work ?
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
Inheritance is one of the more important features in object-oriented programming. Inheritance enables code re-usability, it allows all the features from an existing class(base class) to be inherited by a new class(derived class). In addition, the derived class can also add some features of its own.
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass.
If you have your base Activity and then extend it like so Activity
-> Activity A
it means that when your onCreate of Activity A
is called and you call super.onCreate();
then the onCreate()
method of the original Activity
is also called.
If you then extend Activity A
into Activity B
the calls work like so...
Activity B.onCreate()
- super.onCreate();
-->Activity A.onCreate()
- super.onCreate()
-->Activity.onCreate()
.
Its not a constructor, its a method thats called to create the Activities
. If you then extend them from other Activities
its superclass is going to be called via its super
method. It doesn't mean that the Activities
you have inherited from are going to be created too. Activity B
will be your created Activity
.
EDIT: This page on the Android Developer website is very useful as it visually explains the Android Activity lifecycle.
Yes as a typical java function onCreate should have been inherited but it doens't get : why? - because it's not just a method, it's a life cycle stage.
AFAIK, Activity is not just a java class but it's a special type of a JAVA Class which has it's own life cycle and Life Cycle stages are meant to be called each and every time you use that class/activity even if you have declared or not those methods onCreate(), onPause() etc gets called for sure.
So every time the base activity will get created and destroyed. That's it's nature.
If you have problem with that you can try using Abstract classes, Interfaces and any other public class to have common code inherited in your all activities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With