Understanding the Android Application Class

While starting the android development or while development, we miss out a few of the concepts which doesn’t really affect a lot on the development but if they are known, they could help in debugging and writing beautiful and structured code.
In my coming blogs, I will be writing more about the android stuff which will help you to get better insights on the topics.
What is Application Class?
The Application Class is the predefined class of Android. This class contains all the android activities, services, and other components and is the first class that gets instantiated when the application launches.
Why is it good to create Application Class?
One can create their own application class, simply by extending a class android.app.Application and adding an application tag to their AndroidManifest.xml file.

The onCreate() of the application class is first called before any activity, service, or receiver objects have been created.
The below image states the actual use case of the onCreate().

When your Application class can lead to App Crash?
If you want to store data like auth token, session token or any data which is accessible throughout the application, never store them into the application class object because if you assume that your data will stay there, your application will inevitably crash at some point with a NullPointerException. The application object is not guaranteed to stay in memory forever, it will get killed.
Contrary to popular belief, the app won’t be restarted from scratch. Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place.
Note: Storing data in the Application object is error-prone and can crash your app. Prefer storing your global data in some persistence storage like Shared Preferences if it is really needed later or explicitly pass to your activity in the intent’s extras.
Support
If you like my articles, you can always support me with some money https://www.paypal.me/Abh1sh
Thank you for your attention
You are using your precious time to read my work means the world to me and I really mean that.
If you liked this story, go crazy with the applaud(👏) button and do share this post with your friends and colleagues. It will help other people to find my work and also share it with your friends!
Also, follow me on
