Android 区别以及何时使用 getApplication()、getApplicationContext()、getBaseContext() 和 someClass.this

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10347184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:22:06  来源:igfitidea点击:

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

androidandroid-intentthistoastandroid-context

提问by Pheonix7

I'm new to android and I'm trying to understand the difference between getApplication(), getApplicationContext(), getBaseContext(), getContext()and someClass.thisand especially when to use the these methods in the following code lines:

我是新来的Android和我想明白之间的差别getApplication()getApplicationContext(getBaseContext()getContext()以及someClass.this特别是当使用这些方法在下面的代码行:

When I launch a toast what is the difference between these and in which cases to I use them?

当我启动 Toast 时,这些有什么区别,我在哪些情况下使用它们?

Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();

same with intents:

与意图相同:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);

回答by waqaslam

Toastand Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.thisand getBaseContext, they all offer reference to the context.

ToastIntent都需要参考上下文。而getApplicationgetApplicationContextLoginActivity.thisgetBaseContext,它们都提供对上下文的引用。

Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework.

现在令人困惑的是不同上下文的声明及其特定用法。为简单起见,您应该计算 Android 框架中可用的两种类型的上下文。

  1. Application Context
  2. Activity Context
  1. 应用上下文
  2. 活动上下文

Applicationcontext is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.

应用程序上下文附加到应用程序的生命周期中,并且在应用程序的整个生命周期中始终相同。因此,如果您使用Toast,则可以使用应用程序上下文甚至活动上下文(两者),因为 Toast 可以从应用程序中的任何位置引发并且不附加到窗口。

Activitycontext is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy()is raised. If you want to launch a new activity, you must need to use activity's context in its Intentso that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASKin intent to treat it as a new task.

Activity上下文附加到 Activity 的生命周期,并且可以在onDestroy()引发Activity 时销毁。如果你想启动一个新的活动,你必须在它的Intent 中使用活动的上下文,以便新的启动活动连接到当前活动(就活动堆栈而言)。但是,您也可以使用应用程序的上下文来启动新活动,但是您需要Intent.FLAG_ACTIVITY_NEW_TASK在意图中设置标志以将其视为新任务。

Now referring to your cases:

现在参考你的案例:

LoginActivity.thisthough its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.

LoginActivity.this虽然它指的是您自己的扩展 Activity 类的类,但基类(Activity)也扩展了 Context 类,因此它可以用于提供活动上下文。

getApplication()though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.

getApplication()虽然它指的是 Application 对象,但 Application 类扩展了 Context 类,因此它可以用于提供应用程序上下文。

getApplicationContext()offers application context.

getApplicationContext()提供应用程序上下文。

getBaseContext()offers activity context.

getBaseContext()提供活动上下文。

Tips: Whenever you need to manipulate Viewsthen go for Activity-Context, else Application-Contextwould be enough.

提示:每当您需要操作时,Views请使用 Activity-Context,否则Application-Context就足够了。

回答by AlexR

The answer by Waqas is very clear and complete, however I'd like to further clarify the difference between using thisvs. getBaseContext(), or getApplication()vs. getApplicationContext(). Both Activityand Applicationextend not Contextitself, but ContextWrapper, which is a

Waqas 的回答非常清楚和完整,但是我想进一步澄清使用thisvs.getBaseContext()getApplication()vs.之间的区别getApplicationContext()。双方ActivityApplication延长不是Context本身,而是ContextWrapper,这是一个

"Proxying implementation of Contextthat simply delegates all of its calls to another Context".

“代理实现Context只是将其所有调用委托给另一个Context”。

That "real" context is what you get by using getBaseContext().

使用getBaseContext().

So although this(for Activity) and getBaseContext()both give the activity context, they

因此,尽管this(for Activity) 和getBaseContext()两者都给出了活动上下文,但它们

  • (a) do not refer to the same object (this != getBaseContext()) and
  • (b) calling context through thisis slightly less efficient, as the calls go through an extra level of indirection. I doubt it makes any practical difference, though.
  • (a) 不要指代同一个对象 ( this != getBaseContext()) 和
  • (b) 调用上下文的this效率稍低,因为调用要经过额外的间接级别。不过,我怀疑它是否有任何实际差异。

The same logic applies to getApplication()vs. getApplicationContext().

同样的逻辑适用于getApplication()vs. getApplicationContext().

回答by ngesh

LoginActivity.this 

the above line is an Activity which is obeveously a Context.. this is used when you create some AlertDialogs... At some places its compulsory that you use Activity Context...

上面这行是一个 Activity,它显然是一个 Context.. 这在你创建一些 AlertDialogs 时使用......在某些地方,你必须使用 Activity Context......

getApplication()

Same here the make text method needs Context and Application itself implements Context

同样这里 make text 方法需要 Context 和 Application 本身实现 Context

getApplicationContext()

this is most preferred way since this Contextlives untill Application shuts down.

这是最优选的方式,因为它会一直Context持续到应用程序关闭。

getBaseContext()

this Context is available to widgets and Views..

此上下文可用于小部件和视图..

But All of them gives a Contextobject and nothing else..

但是它们都提供了一个Context对象,没有别的。

回答by chandrakant Chaturvedi

Class.this used if your class extends Activity getapplication() used refer application and application extends application context getbasecontext()refer your activity context context refer to your activity life cycle context applicationcontext refer to your app life cycle

Class.this 如果您的类扩展 Activity getapplication() 使用引用应用程序和应用程序扩展应用程序上下文 getbasecontext() 引用您的活动上下文上下文引用您的活动生命周期上下文应用程序上下文引用您的应用程序生命周期