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

前端 未结 5 1012
花落未央
花落未央 2020-12-29 03:38

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 E

相关标签:
5条回答
  • 2020-12-29 04:09

    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-29 04:13

    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

    0 讨论(0)
  • 2020-12-29 04:17

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

    0 讨论(0)
  • 2020-12-29 04:22

    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

    0 讨论(0)
  • 2020-12-29 04:25

    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
    
    0 讨论(0)
提交回复
热议问题