Skip to content

Instantly share code, notes, and snippets.

@moondroid
Last active October 13, 2015 21:58
Show Gist options
  • Save moondroid/4261555 to your computer and use it in GitHub Desktop.
Save moondroid/4261555 to your computer and use it in GitHub Desktop.
Overriding the Application Lifecycle Handlers
public class MyApplication extends Application {
private static MyApplication singleton;
// Returns the application instance
public static MyApplication getInstance() {
return singleton;
}
//Called when the application is created. Override this method to initialize your
//application singleton and create and initialize any application state variables or shared
//resources.
@Override
public final void onCreate() {
super.onCreate();
singleton = this;
}
//Provides an opportunity for well-behaved applications to free additional
//memory when the system is running low on resources. This will generally only be called when
//background processes have already been terminated and the current foreground applications are
//still low on memory. Override this handler to clear caches or release unnecessary resources
@Override
public final void onLowMemory() {
super.onLowMemory();
}
//An application specifi c alternative to the onLowMemory handler introduced
//in Android 4.0 (API level 13). Called when the run time determines that the current application
//should attempt to trim its memory overhead – typically when it moves to the background.
//It includes a level parameter that provides the context around the request.
@Override
public final void onTrimMemory(int level) {
super.onTrimMemory(level);
}
//Unlike Activities Application objects are not restarted due to
//configuration changes. If your application uses values dependent on specifi c confi gurations,
//override this handler to reload those values and otherwise handle confi guration changes at an
//application level.
@Override
public final void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment