使用情境再掀活动情境、再掀

2023-09-12 09:49:32 作者:追魂剑

要开始,你需要一个Intent的活动,如:

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:

使用 MyActivity.this 或只是 使用 getApplicationContext() 使用 getBaseContext() Use MyActivity.this or just this 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,

这取决于范围。假设,如果你在扩展 应用程序来创建一个全局类创建方法吐司用于在每个类的应用程序您可以使用 getApplicationContext()以创建吧。

It depends on the scope. Suppose if you are creating a method in a global class that extends Application to create a Toast that is used in every class of your Application you can use getApplicationContext() to create it.

如果你想创建一个视图,仅限于特定的活动,您可以使用 Activity.this

If you want to create a view that is restricted to that particular Activity you can use Activity.this

如果你想在一些内部类创建一个AlertDialog也有说法的AsyncTask ,那么你必须使用 Activity.this ,因为 AlertDialog 是链接到活动本身。

Also if you want to create an AlertDialog in some inner class say AsyncTask, then you have to use Activity.this, because the AlertDialog is to be linked to Activity itself.

也不要使用 getBaseContext()只需使用上下文,您有。为了得到更多的信息相同,您可以看到this答 。

Also don't use getBaseContext() just use the Context that you are having. For getting further information for the same you can see this Answer.

所以,答案真正的问题是更好地使用 Activity.this 来开始新的活动

So, the answer to the real question is better to use Activity.this to start a new Activity.

Intent intent = new Intent(Current_Activity.this, Calling.class);
startActivity(intent);