The relationship between Android Context and application

Source: Internet
Author: User

1. I'm beginning to not understand why the explicit intent of this (current activity) is. Because the intent inside of the construction method corresponds to the Context, Class. We found the data back.

  

New Intent (This, anotheraty.  Class);  // called in activity.
 Public Intent (Context packagecontext, class<?> cls) {        new  componentname (Packagecontext, CLS);    }

It is because the activity has inherited the android.content.Context. Here is the official explanation.

An interface for global information about the application environment. This is an abstract class, and its implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as application-level actions such as initiating activities, calling, broadcasting and receiving intentions, and so on.

In fact, the text is the context, you can get global resources. (just learned here, other methods can be used to check the manual)

this. getresources (). getString ("App_name");  // The activity call can get the value defined by the global app_name

2. Android.app.Application class

A base class that needs to maintain global application state. You can provide your own implementation by specifying its name in your androidmanifest.xml. XML <application> tags, which will cause this class to be instantiated when you create the process for your application/package.

For example, if you define a global class and the app inherits the application , you'll set android:name= in the XML <application> tag. App "

This allows you to share the app class with all your activity. in fact, each activity also inherits this application class. The application class also inherits the context class

App app = (APP)this. Getapplicationcontext ();  There  must be a getresources () method in the activity call.

PS: Just learn, record a little. Have a better experience in the future in the update

The relationship between Android Context and application

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.