- ApplicationContext does not have this class, in fact, it should be called: Activity and application in the context of the difference. Well, it is true, when we need context, if it is in the activity, most of the direct transfer of this, when in the anonymous inner class, because this can not be used, Need to write xxxactivity.this, many brothers will be lazy, directly to a getapplicationcontext. So have you ever thought about the difference between xxxactivity.this and getapplicationcontext? The
Xxxactivity.this and Getapplicationcontext return is definitely not an object, one is an instance of the current activity, and one is an instance of the application of the project. Since the difference is so obvious, then the respective usage scenarios must be different, the use of chaos may bring some problems.
-
Use the following table:
Notice that there are some numbers added to the number, which is actually yes in terms of ability, but why no? The following one explains:
Number 1: Starting the activity is possible in these classes, but a new task needs to be created. Generally not recommended.
Number 2: It is legal to go to layout inflate in these classes, but use the system default theme style If you customize some styles that might not be used.
Number 3: allowed when receiver is null, in versions 4.2 or later, to get the current value of a sticky broadcast. (can be ignored)
Note: ContentProvider, broadcastreceiver in the table above because there is a context in its internal method for use.
-
Look at the table, focusing on activity and application, and you can see that the UI-related methods are not recommended or not available in application, and the first three operations are almost impossible to application. In fact, as long as the grasp of a point, any UI-related, should use Activity as the context to deal with, and some other operations, service,activity,application and other instances can, of course, attention to the context of the holding of references, Prevent memory leaks.
The difference between context and ApplicationContext