1

I was having an issue with a handler returning during a screen rotation, meaning that the Activity had not been created yet and causing an error.

I solved this via this post: Static way to get 'Context' on Android?

Which sets up an application level static context to use. I wanted to know how safe this is to use and if there are any instances where I shouldn't use this, as I don't entirely understand it.

Thanks,

Kevin.

Community
  • 1
  • 1
Kevin Pione
  • 299
  • 3
  • 12

4 Answers4

2

No, this is a horrible way to solve it. It means you're using the Application context instead of your Activity context, which is generally wrong and can cause memory leaks. The correct way to do this is to either check for this condition before ding anything that can cause this error, or to catch any exception caused and eat it.

Really the best way to solve this is just to turn off activity recreation on rotation which is and always has been a stupid idiotic idea by Google.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

How safe is doing so?

Keeping an instance of the Application context as a singleton is completely safe, understanding as safe that you will not receive a NPE by accessing this instance. The application object is itself a singleton and there is only one per application. Even when the app is killed by the system, the Application object's onCreate() is the first call, so you will always obtain a reference to the Application context.

Can I use it for everything?

Of course, NO. The application context is a suitable context for resource management, like accessing to string values, getting the database instace and such. But it is not a good target for the Android View framework, which requires an Activity context. So, you can use it always when there is not a view related to its use. LayoutInflater, for example, will produce an error if you pass to it an Application context.

I recommend to use the Activity context always when you can, but there is some cases where passing the activity context is not an option, for example if you are creating a database instance when the application is being started.

What about the activity recreation on rotation?

Every Android developer will tell you it is a complete hell to manage this scenario, and it is better for you to lock your activity in vertical or do avoid the recreation of the activity. Beside there is not consistent way to do it across all brands and devices, there are many posts from Google Engineers advocating against doing it because it may produce serious memory leaks and layout problems. Here you have more information.

Community
  • 1
  • 1
droidpl
  • 5,872
  • 4
  • 35
  • 47
0

If you use application context you cannot do certain things like inflating a layout- if you pass the application Context into the LayoutInflater you will get an Exception.

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
0

Try to use a Loader instead. The result will not be pushed to the views until the Views are ready.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51