Java Question Which type of Context to use...

moster67

Expert
Licensed User
Longtime User
This is really not a question but I have always been unsure which context to use. In B4A, we have ba.context and also ba.applicationContext. When you wrap a library, you can often deduce which context to use from the code you are wrapping. But if you want to write your own library from scratch??

This answer on SO from CommonsWare suggests that getApplicationContext() is almost always wrong. I found also this guide (although from 2013) but probably still valid which I found quite useful to get a better understanding although I still haven't grasped everything:

How do you reason when you need to use a context? Do you have a simple rule?

EDIT: I found this reply from Erel which probably summons it up nicely..
 
Last edited:

moster67

Expert
Licensed User
Longtime User
I guess 99,9% of my wraps are using BA.Context... 0,1% ist using BA.ApplicationContext
I would always start using BA.Context
Yes, I found an answer by Erel. I edited my first post by adding a link to his answer.
So, it seems, as you say, ba.Context is mostly used when you have UI-elements. How is a toast considered? Is it an UI-element?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…