Skip to content Skip to sidebar Skip to footer

What Is The Difference Between Extends Application And Extends Activity In Android?

I am confused as to the difference between the two. In my application I have just used Extends Activity and the application is working perfectly, so what is the purpose of Extends

Solution 1:

The android.app.Application class is an optional facility for extending and storing application-global state. There are other ways of doing this, so most apps don't customize this class.

Activities however are what defines every major stage of your application. It wouldn't be possible to build an application without Activities. You will have a main Activity class and this will indeed be defined with 'extends Activity'.

Solution 2:

Best way to see the difference would be see it's class hierarchy

Activity

java.lang.Object
  ↳ android.content.Context
      ↳ android.content.ContextWrapper
          ↳ android.view.ContextThemeWrapper
              ↳ android.app.Activity

And Application

java.lang.Object
↳   android.content.Context
   ↳    android.content.ContextWrapper
       ↳    android.app.Application

Application is what lives till your android app process is killed. You can use this to stored Application specific data (as long as your application is alive) that may be used across various activities. Note I am not saying you should... Shared preferences may be other appropriate way to go depending on your usecase. Also just to be clear you cannot use your Application to launch your app unlike launcher Activity you give in your manifest file.

You can use your own custom Application class as follows

<applicationandroid:name="icom.osfg.test.app.AppController"android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/MyTheme" ><!-- all the activities goes here --></application>

where

AppController extendsApplication

Solution 3:

Just to add to the previous answers.

The Application class will be a singleton that will live as long as your app is alive.

You could initialize global components in your Application extended class since it will last until your process die if you don't want to handle with the usual Activitylifecycle.

For example, initialization of third party libraries like: Parse, CanaryLeak, Crashlytics.

publicclassAppextendsApplication {

    @OverridepublicvoidonCreate() {
        super.onCreate();

        Parse.initialize(this);
        LeakCanary.install(this);
        Fabric.with(this, newCrashlytics());
    }
}

Solution 4:

application is responsible for whole app

you add launcher activity in application manifest

and

in application on create use to recreate whole app after user's Preference

Solution 5:

Another difference from use case point of view is classes that extend Application usually have the Application context which is required for some system services, like say a RoomDatabase class which wraps around SQLiteOpenHelper so that only one instance can be created (since they are resource heavy) So extends Application in this case

Post a Comment for "What Is The Difference Between Extends Application And Extends Activity In Android?"