1, why to rewrite the application class
If you want to use global variables throughout your application, you typically use static variables, public types in Java, and if you use such global variables in Android that do not conform to Android's framework architecture, but you can use a more elegant way to use application Context
So why is such a global variable not compatible with Android's frame architecture?
I understand that static access cannot be cross-process. Activity,service in Android can be run in their own processes, with static passing parameters to different processes of activity, service will be wrong.
2. How to use the overridden application class
The application class on the source of the general statement that says:
Base class for those who need to maintain global application state. You can
Provide your own implementation by specifying it name in your
Androidmanifest.xml ' s <application> tag, which'll cause that class
To being instantiated for if the process for your application/package is
Created.
Translation: Use this class when we need to maintain global variables. (as explained above, then how to use this class)
You can declare and differentiate this component within the Androidmanifest.xml application tag by the name you take. After the declaration, this class will be instantiated when your application or package is created by the process.
That is, the steps to use the custom application class are:
1) rewrite the application class, such as the name MyApplication. The main rewrite inside of the OnCreate method is to initialize the value of the variable when it is created.
2) Modify the configuration file Applicationmanifest.xml, add the application Appcontext to run:
<application android:allowBackup="true" android:name=".app.AppContext"
3) Instantiating the custom Application object
AppContext app;app = (AppContext)getApplication();
4) Manipulate the variables of the app.
"Android Personal Understanding (iv)" Customizing the Use of the application class