What is the difference between Extends Application and Extends Activity in Android?

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'.


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

<application
    android: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 extends Application

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 Activity lifecycle.

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

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

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

Tags:

Java

Android