Toast:文档的教程说使用 getApplicationContext,但文档参考说使用 getApplication 或 getActivity

Toast: Docs' tutorials say to use getApplicationContext, but Docs References say to use getApplication or getActivity

https://developer.android.com/guide/topics/ui/notifiers/toasts:在来源示例中,他们使用 getApplicationContext.

https://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context,%20int,%20int): "Context: The context to use. Usually your Application or Activity object.".

我们到底应该用什么?

要回答这个问题,我认为应该考虑...:[=​​20=]

https://blog.mindorks.com/understanding-context-in-android-application-330913e32514:这里好像应该用getActivity代替Toast.

What should we use in definitive?

一般来说:如果 Context 被应用到 UI 的某些东西上,请使用 Activity,这样您当前的主题就会被考虑在内。 Toast 是 UI;因此,使用 Activity.

实际上,我认为 Toast 不一定使用主题中的任何内容。

The lifecycle of the object Toast defined in a fragment, and then the lifecycle of Toast defined in an activity

它们是一回事,因为片段与 Toast 无关。您没有将 Fragment 传递给 Toast.

上的任何方法

The risk of memory leaks if we use getActivity, which depends on the Toast

由于 Toast 是 short-lived,任何可能的泄漏都是 short-lived。当泄漏的 material 被无限期引用并且不会被清理时,泄漏是一个问题。