Extending Application

As you develop your Android application, you may find it necessary for certain libraries or settings to be initialized on app startup, rather than during normal application use. This can be done simply by extending the Application class. Simply create a class and have it extend Application, and in the onCreate() method, just initialize the library:

@Override
    public void onCreate() {
        super.onCreate();
        /* Initialization of some cool library */
        SomeLibrary.init();
    }

Now, after we’ve created this class, we need to tell our application to use it! That’s simple too. Just put

android:name=".mypackagename.MyClassThatExtendsApplication"

inside your application tag in your AndroidManifest.xml. This will tell your application to use the class you’ve defined as your Application class, and the OnCreate() method will be run, making sure your app is configured correctly. Another great use case for this is setting up a crash report library. The library will want to start listening for crashes immediately, so the best place to set that up is in the Application class.

Happy coding!
Brandon from The Bunch

Leave a Reply

Your email address will not be published. Required fields are marked *