V.J.
V.J.

Reputation: 9590

Why android gives error when we write getApplicationContext() as a perameter in AlertDialog.Builder.?

I just want to know why android gives error when we write

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

whether it is right. and why it only works when we put this as a perameter.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

Upvotes: 1

Views: 472

Answers (1)

Aswin Kumar
Aswin Kumar

Reputation: 5278

Application is a global object and so is it's Context. Application context should be used only for things that need a Context that is not tied to the currently running component, like an Activity.

In this case, the AlertDialog is created inside the Activity (i assume) and it needs the context of only that Activity - hence you should use this. The reasoning is that AlertDialog does not have a life outside of its parent Activity.

Upvotes: 3

Related Questions