Reputation: 1219
Hello I am noob in android. I see that in android you can get context using different method. I could not understand the difference between them and when to use what .
Methods : getApplicationContext()
, getContext()
, getBaseContext()
, this (Activity)
Upvotes: 0
Views: 82
Reputation: 2744
An 'Application context' is associated with the Application and will always be the same throughout the life cycle of your app (getApplicationContext())
The 'Activity context' is associated with the activity and could possibly be destroyed many times as the activity is destroyed during screen orientation changes and such.(getContext())
Generally don't use getBaseContext(), rather use one of the previous ones as needed.
You might want to use the Application Context (Activity.getApplicationContext()) rather than using the Activity context (this). This is because 'this' needs to be called from within an Activity. (Activity extends Context )
Upvotes: 2