"Android Personal Understanding (iv)" Customizing the Use of the application class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.