The difference between context and ApplicationContext

Source: Internet
Author: User

    • 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

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.