Inheritance between activities in Android
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 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.