difference between GetContext (), Getapplicationcontext (), Getbasecontext ()

Source: Internet
Author: User

1. http://stackoverflow.com/questions/22966601/ what-is-different-between-mainactivity-this-vs-getapplicationcontext/22967165#22967165

There is types of the Context:

Application Context is associated with the application and would always being same throughout the life of application --it does not a change. If you are using a toast, you can use application context or even activity context (both) because Toast can displayed From anywhere with the your application and is not attached to a specific window. But there was many exceptions, one exception is, the activity context for you need to use or pass.

activity Context is associated with to the activity and can being destroyed if the activity is destroyed--there MA Y is multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should-launch a new activity, you need-to-use activity context-its-Intent so, the new launching AC Tivity is connected to the current activity in terms of activity stack. However, application ' s context too to launch a new activity, and then you need to set flag intent.flag_activity _new_task in intent to treat it as a NEW TASK.

Let ' s consider some cases:

Mainactivity.this refers to the mainactivity context which extends activity class but the base class (activity) also exten DS Context class, so it can is used to the offer activity Context.

Getbasecontext () offers activity context.

Getapplication () offers application context.

Getapplicationcontext () also offers application context.

For more information please check the this link.

2. http://stackoverflow.com/questions/10641144/ difference-between-getcontext-getapplicationcontext-getbasecontext-and/10641257#10641257

    • View.getcontext (): Returns The context the View is currently running in. usually the currently active Activit Y.

    • Activity.getapplicationcontext (): Returns The context for the entire application (the process all the activities is running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, no t just the current Activity.

    • Contextwrapper.getbasecontext (): If You need access to a context from within another context, you use a CONTEXTWR Apper. The Context referred to from inside this contextwrapper is accessed via Getbasecontext ().

difference between GetContext (), Getapplicationcontext (), Getbasecontext ()

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.