Difference between this and getapplicationcontext Context

Source: Internet
Author: User
From: http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context

In various bits of Android code I 've seen:
In a large amount of Android code, I see the following statement:

Public class myactivity extends activity {
Public void method (){
Mcontext = This; // Since activity extends Context
Mcontext = getapplicationcontext ();
Mcontext = getbasecontext ();
}
}

However I can't find any decent explanation of which is preferable, and under what circumstances which shocould be used.
However, I cannot find any decent explanation: that is the best way to write, and in which case should I use it.

Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, wocould be much appreciated.
I would be grateful if you have an address for this article, or if you have chosen a wrong writing method, it will lead to an error.

I agree that documentation is sparse when it comes to contexts in Android, but you can piece together a few facts from varous sources.
I admit that articles related to contexts in Android are rare, but we can still piece together things from various sources.

This blog post on the official Google Android Developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
This blog is written by Google's official Android developer mainly to help solve memory leaks. It also provides contexts with some good information:

In a regular Android Application, you usually have two kinds of context, activity and application.
In a common android application, you usually have two types of context, activity, and application.

Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application context (activity. getapplicaitoncontext () rather than using the activity context ("this "). basically the application
Context is associated with the applicaiton and will always be the same throughout the life cycle of you app, where as the activity context is associated with the activity and cocould possible be destroyed stored times as the activity is destroyed during Screen
Orientation changes and such.
If you read this article in depth, it tells us the differences between the two contexts, and why do you consider using the application context (activity. getapplicaitoncontext () instead of using the active context (that is, "this "). Basically, the application context is associated with the application, and is always the same in the life cycle of the program. However, the activity context is associated with an activity. With the activity destruction (such as switching between landscape and landscape), the context will be destroyed multiple times.

I couldn't find really anything about when to use getbasecontext () Other than/from one/(A for a) liner from Dianne hackborn, one of the Google engineers working on the android SDK:
I really didn't find anything about using getbasecontext () except one sentence in Dianne hackborn (a Google engineer who compiled the android SDK:

Don't use getbasecontext (), just use the context you have.
Do not use getbasecontext (), but only use your own context.

That was from a post on the Android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
It is an email from the Android-developers newsgroup. You can also consider asking questions there, because a large number of Android Developers are managing this group and answering questions.

So overall it seems preferable to use the global application context when possible.
Therefore, in general, it seems better to use the global application context whenever possible.

I got bad window token errors when using application context to progress dialog. So I used activity context instead.
The "window token errors" error occurs when I use the application context to send the progress bar dialog box. Therefore, I use the active context instead of the application context.

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.