sjso
sjso

Reputation: 297

What is an example where you use getBaseContext()?

if i understand correctly, you use getContext() to get the context of a view object. You use activity context or this if you want to create a new object that exist as long as activity exist, if not the activity wont be destroyed and eventually lead to memory leaks. You use getApplicationContext() if you want a context that is tied to the global lifecycle of the application and should be used wherever we need to create a object that exist beyond the current activity.

Is my understanding correct ?

But what is getBaseContext() and where do you use it exactly.. One simple and solid example would be great

Upvotes: 3

Views: 1097

Answers (1)

CyamiAnbu
CyamiAnbu

Reputation: 41

View.getContext() usually returns the context the view of current Activity. ContextWrapper.getBaseContext()is used while accessing a Context from within another context example Activity.

See at - [Difference between getContext() , getApplicationContext() , getBaseContext() and] "this"7]: Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Upvotes: 1

Related Questions