What is the correct way to pass context reference between classes?

In my application, I am referencing context in various places including Intents, resource access, file operations, AlertDialog, etc. And also its difficult to convey the current context in different classes. So I tried to set the base application context in the singleton class from the launcher activity. This way, when I start the application, I get the application context and set it in the class. Later, I mean the same instance of this context. But in some places like AlertDialog

it gives an error if I use the underlying context. So, is it a good idea to keep the context in a shared place and access it instead of passing the current context between different classes? Or what is the preferred way to transfer context when communicating between different classes?

This is my class for getting context

public class AppContext extends Application {
    private static Context sContext = null;
   @Override
    public void onCreate() {
        super.onCreate();
        sContext = getApplicationContext();
    }

    public static Context getContext() {
        return sContext;
    }
}

      

+3


source to share


2 answers


But in some places like AlertDialog it gives an error if I use the base context

You are not allowed to show dialogs from the application context. You can only do this with context Activity

.

So, is it a good idea to keep the context in a common place and access it instead of passing the current context between different classes?

It depends on the type of context. If it is an application context, you can keep it in singleton mode if you like. If it is a context Activity

, you must pass it by reference and be careful when storing this reference somewhere to avoid memory leaks.



Or what is the preferred way to transfer context when communicating between different classes?

You can pass as a parameter Context

. It's ok to keep a link to this Context

one if it's safe to do so.

For example, it is unsafe to launch AsyncTask

and hold a Activity

context reference from there because it AsyncTask

has its own lifecycle and this will prevent it from being removed Activity

from the garbage collected when the screen is rotated. This will cause a memory leak. In this case, it is better to have WeakReference

an activity context.

So my point is that you can pass the context as a parameter wherever you want. If you want to keep a link to context, be careful.

+4


source


You could just create a setter method that takes parameters Context

for its parameters and then inserts this

for its Context into the class you want .

From there, you will be able to use this Context

no matter what class you are in, because you saved it as a variable.

Optional, if you don't want to do this, you can also do this:



public static Context contextA;

    @Override
    public void onCreate()
    {
        super.onCreate();
        contextA = this;
    }

      

Both should work fine.

-1


source







All Articles