What's the difference between the various methods to get a Context?

前端 未结 8 682
情书的邮戳
情书的邮戳 2020-11-22 04:06

In various bits of Android code I\'ve seen:

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activi         


        
8条回答
  •  萌比男神i
    2020-11-22 05:03

    The confusion stems from the fact that there are numerous ways to access Context, with (on the surface) no discernible differences. Below are four of the most common ways you may be able to access Context in an Activity.

    getContext()
    getBaseContext()
    getApplicationContext()
    getActionBar().getThemedContext() //new
    

    What is a Context? I personally like to think of Context as the state of your application at any given time. The application Context represents a global or base configuration of your application and an Activity or Service can build upon it and represents a configuration instance of your Application or a transitive state for it.

    If you look at the source for android.content.Context, you see that Context is an abstract class and the comments on the class are as follows:

    Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc. What I take away from this is that Context provides a common implementation to access application level as well as system level resources. Application level resources may be accessing things like String resources [getResources()] or assets [getAssets()] and system-level resource is anything that you access with Context.getSystemService().

    As a matter of fact, take a look at the comments on the methods and they seem to reinforce this notion:

    getSystemService(): Return the handle to a system-level service by name. The class of the returned object varies by the requested name. getResources(): Return a Resources instance for your application’s package. getAssets(): Return a Resources instance for your application’s package. It may be worth pointing out that in the Context abstract class, all of the above methods are abstract! Only one instance of getSystemService(Class) has an implementation and that invokes an abstract method. This means, the implementation for these should be provided mostly by the implementing classes, which include:

    ContextWrapper
    Application
    Activity
    Service
    IntentService
    

    Looking at the API documentation, the hierarchy of the classes looks like this:

    Context

    | — ContextWrapper

    |— — Application

    | — — ContextThemeWrapper

    |— — — — Activity

    | — — Service

    |— — — IntentService

    Since we know that Context itself is not providing any insight, we move down the tree and take a look at the ContextWrapper and realize that there isn't much there either. Since Application extends ContextWrapper, there isn't much to look at over there either since it doesn't override the implementation provided by ContextWrapper. This means that the implementation for Context is provided by the OS and is hidden from the API. You can take a look at the concrete implementation for Context by looking at the source for the ContextImpl class.

提交回复
热议问题