February 05, 2016

Android Dev Tips - How to get static Application Context from anywhere?

Everyone is facing with the problem of accessing Context instance from methods, which aren't logically linked with Activity or Service.

It may be needed for accessing independent resources, like strings or files from assets folder. Sometimes it may be inconvenient to initialize and keep additional variable with the static context.

There're lot of questions on StackOverflow about this. Today I want to share my experience of handling such kind of problems.


So, the first solution is just to place context variable in target class. Unfortunately it may affect lot of related code, we need to pass Context reference across all of dependent classes.


Other guys try to add public static method to Application class and access it from anywhere. I don't like this approach because of additional dependency to custom Application class. In case when you're working on library, it's inappropriate at all.

Well, my solution is to create standalone class AppHolder which will be able to provide us Application Context without any additional steps.


This code access hidden field from OS class via Reflection API. Of course, it's not good and it may be risky in theory, but it works well on practice! This code is executed only once per lifetime of app. Static reference to Application instance is cached in static final variable, nobody will re-write it.

If you don't like to use Reflection API at all, you may adapt this solution on your own choice.

And now we can access static application context from anywhere.


You may find source code here or you can use method Global.getContext() from my Xdroid library. This library is still "in progress", but it was tested already in real projects.

dependencies {
    compile 'com.shamanland:xdroid-core:0.3.0'
}



Thanks for reading! Watch for updates.

8 comments:

  1. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better. The post is written in very a good manner and it contains much useful information for me. Thank you very much and I will look for more postings from you.

    digital marketing blog
    skartec's digital marketing blog
    skartec digital marketing academy
    skartec digital marketing
    best seo service in chennai
    best seo services in chennai

    ReplyDelete
  2. Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging.Thank you for sharing wonderful information with us to get some idea about that content.oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete
  3. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    Artificial Intelligence Training in Chennai

    Ai Training in Chennai

    Artificial Intelligence training in Bangalore

    Ai Training in Bangalore

    Artificial Intelligence Training in Hyderabad | Certification | ai training in hyderabad

    Artificial Intelligence Online Training

    Ai Online Training

    Blue Prism Training in Chennai

    ReplyDelete