Mick
Mick

Reputation: 1219

Context Methods in Android

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

Answers (1)

Ushal Naidoo
Ushal Naidoo

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

Related Questions