Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to save the app context to a static variable in Android?

I know that usage of static variables on Android is quite risky, especially if you reference them to activities. However, if I have a class that extends Application (let's call this class "App"), is it safe to reference to the instance of this class?

If so, is it also safe for any other class to have any kind of reference to the application context? I mean, can there be a memory leak if I have a reference to the application context in any kind of class?

The purpose is that no matter in which scope I am in, I can always get a reference to the application context. I think it's safe, since if the system closes the application, the static variable is also gone till the next time the application starts again, which will initialize the static variable again.

Also, not that it matters much, but if I use multiple processes, will I get totally different references to App class on each process?

As an example of code, here's what I'm thinking about:

public class App extends Application {     private static Context _appContext;      @Override     public void onCreate()     {         super.onCreate();         _appContext = this;     }      public static Context getAppContext()     {         return _appContext;     } } 
like image 378
android developer Avatar asked Apr 21 '12 11:04

android developer


People also ask

Is static variable safe?

Static variables are not thread safe. Instance variables do not require thread synchronization unless shared among threads. But, static variables are always shared by all the threads in the process. Hence, access to static variable is not thread safe.

What is static variable in Android?

Static variables are also known as Class variables which are declared with the “static” keyword in a class. A single copy of each variable per class is to be shared by all instances of the class. Static variables are stored in static memory.

What should I pass in context android?

It is used to return the Context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context.

What is Android content context?

Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).


1 Answers

is it safe to save the app context to a static variable?

Presently, yes, it appears to be safe, though I would not have getAppContext() return Context, but instead return App or Application.

That being said, the fact that the core Android team did not set it up this way in the first place suggests that perhaps there may be hidden issues of which we are unaware, or that in the future this approach may introduce problems.

As the acronym of the saying goes, YMMV. :-)


EDIT

if so , is it also safe for any other class to have any kind of reference to the application context ?

I have no idea what you mean by "safe" here.

but if i use multiple processes , i will get totally different references to App class on each process , right?

If you use multiple processes, you should be slapped with a trout. But, yes, you should get distinct App instances per process.

like image 98
CommonsWare Avatar answered Sep 19 '22 10:09

CommonsWare