Levi Omer Levi Omer - 3 years ago 174
Android Question

How to access a start of Application (not 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{
 @Override
    public void onCreate() {
        super.onCreate();
        //Do whatever you want 
    }
}

And this AppApplication class should be included in manifest file.

<application
        android:allowBackup="true"
        android:name=".AppApplication"
        android:icon="@mipmap/ic_launcher"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download