Thursday, July 19, 2012

Where to put android app start-up initialization

There is no 'main' function in Android app. There is a main activity that get lunched when user clicks on app's icon, but other activities can be started as well. So, if the app need some setting up at start up, where should that be in. It turns out that you can have a main entry point that got called every time the program starts.  Simply put initialization code there and it's all good.

To use it, you have to create a new class that extends android.app.Application. Within it you have to override onCrate method. This method will get called when app starts.
public class MyMainEntryPoint extends Application { 

    @Override
    public void onCreate() {
        super.onCreate();
        // init goes here
    }
}
Then, in AndroidManifest.xml, you have to declare your entry point using android:name attribute under application tag
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:name=".MyMainEntryPoint">

  ...

</application>

No comments:

Post a Comment