开发者

Android toast class parameter

开发者 https://www.devze.com 2023-04-07 17:29 出处:网络
I had doubt in android Toast class. We have a static method in android called makeText and have to use it as

I had doubt in android Toast class. We have a static method in android called makeText

and have to use it as

Toast.makeText(Context context , CharSequence text, int dur开发者_如何学编程ation);

and we can use getBaseContext() as one of the options among getBaseContext() , getParent() and getApplicationContext().

Please anyone can explain me the purpose and usage of getBaseContext()

Thank you........


To be able to show the message you need to have some reference to the context. Toast is static class you can call Toast from any activity, broadcast receiver or any class that have context

In general there are two type of classes. Ones that extend ContextWrapper class (Activity, Service, Application) and those that do not extend it (like View).

If class extends ContextWrapper then you can use this as Context. Such classes normally do not have getContext() method.

Those classes that do not extend ContextWrapper but still save and use Context normally expose getContext() function. And you cannot use this as Context in such cases.

And these two cases are mutually exclusive. At least I don't recall classes that extend ContextWrapper and have getContext at the same time.

this for more details What's the difference between the various methods to get a Context?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号