Rewrite Application class for android Development
In android Application Development, rewriting Application is also quite common. Some previously developed programs are too simple to rewrite this class, but in real commercial development, rewriting the Application class is almost required.
Why rewrite the Application?
1. Process global variables and share data throughout the application
2. Manage the Activity. When the application exits, destroy all the activities.
3. initialize application configuration information
4. When the system memory is insufficient, the application can make a reasonable response.
.....
After inheriting the Application, you must declare it in the configuration file, that is, specify the name attribute of the Application node:
Override Application class:
/*** Note that the superclass event handler must be called during method rewriting * @ author Wang song * @ date July 22, July 30, 2015 */public class MyApplication extends Application {private String globalVar; private MyApplication instance; private List activities;/*** Singleton mode * @ return */public MyApplication getInstance () {if (instance = null) return new MyApplication (); return instance;}/*** add activity * @ param a */public void addActivity (Activity a) {activities. Add (a);}/*** traverse all the activities and finish */public void finishActivity () {for (Activity: activities) {if (activity! = Null &&! Activity. isFinishing () {activity. finish () ;}} public String getGlobalVar () {return globalVar;} public void setGlobalVar (String globalVar) {this. globalVar = globalVar;}/*** called when creating an application. You can override this method to instantiate the single State of the application, and create and instantiate any application * program state variables or shared resources */@ Override public void onCreate () {super. onCreate (); activities = new rule list ();}/*** as an alternative to onLowMemory specific to applications, introduced in android4.0, * when running the program, the current application should be reduced. When the memory overhead is reduced (usually when it enters the background), call * which contains a level parameter to provide the request context */@ Override public void onTrimMemory (int level) {super. onTrimMemory (level);}/*** is different from Activity. When the configuration changes, the application object will not be terminated or restarted. * If the values used by the application depend on specific configurations, rewrite this method to reload these values, or change these values at the application level */@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig);}/*** when the system is in a shortage of resources, applications with good behaviors can release additional memory. * This method is generally called only when the background process is terminated, but the foreground application still lacks memory. * We can rewrite this program to clear the cache or release unnecessary resources */@ Override public void onLowMemory () {super. onLowMemory ();}}
Comments are used to describe the key points.
So how to use this class in Activity?
MyApplication mp = (MyApplication) getApplication (); mp. setGlobalVar (Zhang San );
This is simple, but in most cases we will use Map to store global variables. Here we use String directly for simplicity.