Levi Omer Levi Omer - 3 years ago 99
Android Question

How to access the start of the application(not the activity)

I need to implement very specific code in the start of the application.

I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.

I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities.
This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.

I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.

Therefor, I need to access the start of the application, or that you have another solution for me.

Thank you !

Answer Source

The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created. You need to extend application class.

public class AppApplication extends Application{
    public void onCreate() {
        //Do whatever you want 

And this AppApplication class should be included in manifest file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download