Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance between activities in Android

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 ?

like image 596
AndroidLearnMachine Avatar asked Aug 30 '13 06:08

AndroidLearnMachine


People also ask

Why do we need to call setContentView () in onCreate () of activity class?

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.

Why do we use inheritance in 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.

What is multiple inheritance Android?

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.


2 Answers

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.

like image 61
StuStirling Avatar answered Oct 06 '22 07:10

StuStirling


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.

like image 34
MKJParekh Avatar answered Oct 06 '22 07:10

MKJParekh