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.
Popular Fashion Blogs in Surat
ReplyDeleteFashion Blogger in Surat
Surat Blogger
Indian Fashion Blogger
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.
ReplyDeletedigital marketing blog
skartec's digital marketing blog
skartec digital marketing academy
skartec digital marketing
best seo service in chennai
best seo services in chennai
Great article it has so much of information so interesting to read it.
ReplyDeleteacte reviews
acte velachery reviews
acte tambaram reviews
acte anna nagar reviews
acte porur reviews
acte omr reviews
acte chennai reviews
acte student reviews
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
ReplyDeleteoracle 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
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!
ReplyDeleteArtificial 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
nicepost
ReplyDeleteSoftware Testing Training in Chennai | Certification | Online
Courses
Software Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
Amazing content and is well managed through this blog and Thanks for sharing this information
ReplyDeleteFull Stack Course Chennai
Full Stack Training in Bangalore
Full Stack Course in Bangalore
Full Stack Training in Hyderabad
Full Stack Course in Hyderabad
Full Stack Training
Full Stack Course
Full Stack Online Training
Full Stack Online Course
Persuading the companies to trust Salesforce thanks to its scalability and customization makes this company take the leading position among CRM services providers.
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
Nice article, much obliged for more data The data you gave about has been useful while examining the subject.graphic design company in chennai
ReplyDeletePlease keep sharing this types of content, really amazing. Please follow my website for more information in Best Event Management Company in Kolkata.
ReplyDeleteThis is stunning and fascinating to experience this blog, truly decent introduction and it clarifies everything in detail, social media marketing agency in chennai thanks for your post! Truly fascinating websites branding companies in chennai .
ReplyDelete