To start an Activity you need an Intent, like:
Intent i = new Intent(context, class)
So to fill in the context parameter, a couple of options are available:
- Use
MyActivity.thisor justthis - Use
getApplicationContext() - Use
getBaseContext()
And I’m sure there are one or two more options.
These options all appear in some sort of tutorial, one uses the first, the next uses the third option.
So which one should I use? Does it even matter? Is it different for different cases?
Yes its different for different cases,
It depends on the scope. Suppose if you are creating a method in a global class that
extendsApplicationto create aToastthat is used in every class of yourApplicationyou can usegetApplicationContext()to create it.If you want to create a view that is restricted to that particular Activity you can use
Activity.thisAlso if you want to create an AlertDialog in some inner class say
AsyncTask, then you have to useActivity.this, because theAlertDialogis to be linked toActivityitself.Also don’t use
getBaseContext()just use theContextthat you are having. For getting further information for the same you can seethis Answer.So, the answer to the real question is better to use
Activity.thisto start a newActivity.