android - 用于 toast 的上下文

标签 android

在很多示例中,我注意到它们是这样创建的:

Toast.makeText(getApplicationContext(), "Toast Text", Toast.LENGTH_SHORT).show();

一般来说,我习惯只对我所在的 Activity 使用“this”关键字,而且效果非常好。

是否有任何令人信服的理由支持使用应用程序上下文?

最佳答案

前面的答案根本不正确。在这个类似的帖子中查看 CommonsWare 对这个问题的回答:

When to call activity context OR application context?

您最常想使用“this”(这是 Activities 上下文)。

关于android - 用于 toast 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7773308/

相关文章:

android - 如何使用用户名/密码通过 Devise 和 Android 对用户进行身份验证?

android - Flutter Base64编码字符串不完整

Android - 将自定义可绘制背景设置为 ClickSpan

android edittext单击按钮后删除焦点

android - 可扩展的 ListView ,如 android 中的 TreeView

android - 在多 fragment Activity 中使用选项菜单

android - gson.fromjson 返回 null

android - 将 MountSpec 组件与 "wrap_content" View 一起使用 - 如何实现 onMeasure()

android - 未能安装以下 Android SDK 包,因为某些许可证未被接受。- 20.1.5948944 NDK(并排)20.1.5948944

java - Android 绘制 2D 图形