android

Why does Toast "Explicitly" require context parameter?


Why does Toast need Context?

Yes, I get that a context parameter is required for accessing resource, and app-specific informations, layoutinflator.

  1. accessing resources and app-specific informations -> this can be done with ApplicationContext. Can't see why you'd want to add more parameter. It'd be more simple to use ApplicationContext by default internally.

  2. LayoutInflators -> this can be also accessed through ApplicationContext.

It would make sense if behavior varies depending on context value, but as far as I know, Toast function isn't one of those case.

So what I want to know is, "Why is the context parameter Explicitly required?"


Solution

  • this can be done with ApplicationContext

    I am assuming that you mean the Application singleton instance of Context. That is not publicly available in isolation. You get the Application singleton by calling getApplicationContext() on a Context. So, whether Toast uses the Context directly or uses it to get the Application, Toast will need a Context.

    As with the rest of the Android Open Source Project, the source code to Toast is publicly available, so you can see what specific uses Toast makes of the supplied Context.