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

前端 未结 5 1025
花落未央
花落未央 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());
        }
    }
    

提交回复
热议问题