What is the difference between methods for obtaining a context? [duplicate]

7

You can, in addition to this , get the context in various ways, with different methods. As shown in the code below, we have getApplicationContext() and getBaseContext() that apparently has the same purpose.

public class MainActivity extends AppCompatActivity{

    public Context context;

    public void umMetodoQualquer() {
        context = this;
        context = getApplicationContext();
        context = getBaseContext();
    }
}

And in practice, what are the differences between methods for getting context?

    
asked by anonymous 27.01.2017 / 13:35

3 answers

8

They return different "types" of Context, with different lifetimes and access to resources .

Context is an abstract class, implemented internally by class ContextImpl . The various "types" (behaviors) of Context are obtained by using factory methods from ContextImpl.

A Context is publicly available through inherited classes from ContextWrapper . ContextWrapper inherits from Context and implements its methods to simply delegate calls to another Context (ContextImpl).
This allows subclasses, such as Application, Service, and Activity (indirectly through ContextThemeWrapper ), modify the its behavior without changing the original Context.

  • this - In this case refers to an Activity. Your lifetime is linked to the lifetime of Activity and through it you access the resources (1) defined for this Activity.

    java.lang.Object
       ↳    android.content.Context
           ↳    android.content.ContextWrapper
               ↳    android.view.ContextThemeWrapper
                   ↳    android.app.Activity  
    
  • getApplicationContext() - Refers to the application. Its lifetime is linked to the lifetime of the application, and through it, it accesses the resources (1) defined for the application.

    java.lang.Object
       ↳    android.content.Context
           ↳    android.content.ContextWrapper
               ↳    android.app.Application 
    
  • getBaseContext() - Returns the context that was used when creating the ContextWrapper object, passed to the constructor, or assigned to it by the attachBaseContext() method. It is of type ContextImpl.

    java.lang.Object
       ↳    android.content.Context
           ↳    android.content.ContextImpl
    

    If the ContextWrapper subclass does not overwrite any of the methods, the Context returned will have a behavior equal to that of the subclass. For example, the context passed to the onReceive() method of BroadcastReceiver is of type ReceiverRestrictedContext . The registerReceiver() and bindService() methods are overwritten to launch ReceiverCallNotAllowedException, if called.

For instance, if for this Activity you set a theme theme other than the theme of the application, views created with the context this look different than those created with the context returned by getApplicationContext() .

You can check the type of each Context, obtained by each of the methods, using this code:

activity = this;
applicationContext = getApplicationContext();
baseContext = getBaseContext();

Log.d("Context", activity.getClass().getName());
Log.d("Context", applicationContext.getClass().getName());
Log.d("Context", baseContext.getClass().getName());
    
28.01.2017 / 01:26
7

getApplicationContext() as it says the name is associated with the application context and will be the same for the duration of the application.

Already getBasecontext() is associated with the activity and will be destroyed along with the activity.

    
27.01.2017 / 13:43
4

this : It is the current context of your activity, it belongs to activiy, when the activiy is destroyed, the context will also be. Note: It extends from Activity , which can also extend from a Context class.

getApplicationContext() : It is the context of the application, the entire application life cycle, when the application is destroyed, then the application context will also be.

getBaseContext() : This is a ContextWrapper method, it is the simplest context implementation that delegates all calls to another context. It can be subclassified to modify behavior and change the original context.

When to use:

Use this : When you need to display Dialogs, AlertDialogs, etc., messages to the user in the Activity that he is in.

Use getApplicationContext : Use when you want a context that is independent of the Activity lifecycle, that is, throughout the application cycle.

Use getBaseContext : When you want to access Context from another context within the application you can access. Example: Widgets, Views, etc.

    
27.01.2017 / 13:58