Android MainActivity.this 与 getApplicationContext() 有什么不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22966601/
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
What is different between MainActivity.this vs getApplicationContext()
提问by AndyError
I am trying ProgressDialog.But I am confuse.
我正在尝试 ProgressDialog。但我很困惑。
1.pd=ProgressDialog.show(MainActivity.this, "", "Fething data");
1.pd=ProgressDialog.show(MainActivity.this, "", "Fething data");
when I do use (MainActivity.this)then it is ok. But
当我使用(MainActivity.this) 时就可以了。但
2.pd=ProgressDialog.show(getApplicationContext(), "", "Fething data");
2.pd=ProgressDialog.show(getApplicationContext(), "", "Fething data");
When I do use (getApplicationContext())it is ERROR.
当我使用(getApplicationContext()) 时,它是错误的。
What is problem for this progressDialog?
这个progressDialog有什么问题?
What is different between (MainActivity.this
) vs (getApplicationContext()
)
( MainActivity.this
) 与 ( getApplicationContext()
) 有什么不同
and when I use it perfect time?
当我使用它的完美时间?
For getApplicationContext()
Error is:
对于getApplicationContext()
错误是:
04-09 15:05:37.453: E/AndroidRuntime(9980): FATAL EXCEPTION: main
04-09 15:05:37.453: E/AndroidRuntime(9980): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.app.Dialog.show(Dialog.java:281)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.app.ProgressDialog.show(ProgressDialog.java:116)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.app.ProgressDialog.show(ProgressDialog.java:99)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.app.ProgressDialog.show(ProgressDialog.java:94)
04-09 15:05:37.453: E/AndroidRuntime(9980): at com.example.shikkok_services.MainActivity.onClick(MainActivity.java:27)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.view.View.performClick(View.java:4204)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.view.View$PerformClick.run(View.java:17355)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.os.Handler.handleCallback(Handler.java:725)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.os.Looper.loop(Looper.java:137)
04-09 15:05:37.453: E/AndroidRuntime(9980): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 15:05:37.453: E/AndroidRuntime(9980): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:05:37.453: E/AndroidRuntime(9980): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:05:37.453: E/AndroidRuntime(9980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 15:05:37.453: E/AndroidRuntime(9980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 15:05:37.453: E/AndroidRuntime(9980): at dalvik.system.NativeStart.main(Native Method)
回答by Zohra Khan
Which context to use?
There are two types of Context:
有两种类型的上下文:
Application contextis associated with the application and will always be the same throughout the life of application ; it does not change. So if you are using Toast
, you can use application context
or even activity context
(both) because Toast
can be displayed from anywhere within your application and is not attached to a specific window. But there are many exceptions. One such exception is when you need to use or pass the activity context
.
应用程序上下文与应用程序相关联,并且在应用程序的整个生命周期中始终相同;它不会改变。因此,如果您正在使用Toast
,则可以使用application context
或 甚至activity context
(两者),因为Toast
可以从应用程序中的任何位置显示并且不附加到特定窗口。但也有很多例外。一个这样的例外是当您需要使用或传递activity context
.
Activity contextis associated with the activity and can be destroyed if the activity is destroyed ; there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new Activity
, you need to use activity context in its Intent
so that the newly launched activity is connected to the current activity in terms of activity stack. However, you may also use application's context to launch a new activity, but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK
in intent to treat it as a new task.
活动上下文与活动相关联,如果活动被销毁,则可以被销毁;一个应用程序可能有多个活动(很可能)。有时您绝对需要活动上下文句柄。例如,如果您启动一个新的Activity
,您需要在其中使用活动上下文,Intent
以便新启动的活动在活动堆栈方面连接到当前活动。但是,您也可以使用应用程序的上下文来启动新活动,但是您需要Intent.FLAG_ACTIVITY_NEW_TASK
在意图中设置标志以将其视为新任务。
Let's consider some cases:
让我们考虑一些情况:
MainActivity.this
refers to the MainActivity
context which extends Activity
class but the base class (Activity
) also extends Context
class, so it can be used to offer activity context.
MainActivity.this
指的是MainActivity
扩展Activity
类的上下文,但基类 ( Activity
) 也扩展了Context
类,因此它可用于提供活动上下文。
getBaseContext()
offers activity context.
getBaseContext()
提供活动上下文。
getApplication()
offers application context.
getApplication()
提供应用程序上下文。
getApplicationContext()
also offers application context.
getApplicationContext()
还提供应用程序上下文。
For more information please check this link.
有关更多信息,请查看此链接。
回答by Varun
MainActivity.this
only works if you are in an inner class ofMainActivity
.If you are in
MainActivity
itself, just usethis
.If you are in
another class
entirely, you need to pass it aninstance
of acontext
from theActivity
you are in.
MainActivity.this
仅当您在MainActivity
.如果你
MainActivity
自己,只需使用this
.如果你
another class
完全在里面,你需要从你所在的地方传递一个instance
a 。context
Activity
Hope this helps..
希望这可以帮助..
回答by triggs
This explanation is probably missing some subtle nuances but it should give you a better understanding of why one works but the other doesn't.
这种解释可能遗漏了一些微妙的细微差别,但它应该让您更好地理解为什么一个有效而另一个无效。
The difference is that MainActivity.this
refers to the current activity (context
) whereas the getApplicationContext()
refers to the Application class.
区别在于MainActivity.this
指的是当前活动 ( context
) 而getApplicationContext()
指的是 Application 类。
The important differences between the two are that the Application class never has any UI associations and as such has no window token.
两者之间的重要区别在于 Application 类从不具有任何 UI 关联,因此没有窗口标记。
Long story short: For UI items that need context, use the Activity.
长话短说:对于需要上下文的 UI 项,请使用 Activity。
回答by Bhargav Ghodasara
MainActivity.this refers to the the current activity (context) where the getApplicationContext() refers to the Application class.
MainActivity.this 指的是当前活动(上下文),其中 getApplicationContext() 指的是 Application 类。
The getApplicationContext() method return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
getApplicationContext() 方法返回当前进程的单个全局 Application 对象的上下文。这通常仅在您需要生命周期与当前上下文分开的上下文时才应使用,该上下文与流程的生命周期而非当前组件相关联。
MainActivity.this will change when activity destroyed and re-created, getApplicationContext() will change when the application killed and re-started.
MainActivity.this 将在活动销毁和重新创建时更改,getApplicationContext() 将在应用程序终止并重新启动时更改。
回答by SAURABH OMER
mainActivity
gives the context of the current Activity. context
depends on activity's lifecycle. getApplicationContext()
gives the context of the application and depends on the application's lifecycle.
mainActivity
给出当前活动的上下文。context
取决于活动的生命周期。getApplicationContext()
给出应用程序的上下文并取决于应用程序的生命周期。
回答by andMarkus
This is what the developer.android.com says:
这是 developer.android.com 所说的:
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
返回当前进程的单个全局 Application 对象的上下文。这通常仅在您需要生命周期与当前上下文分开的上下文时才应使用,该上下文与流程的生命周期而非当前组件相关联。
In general, use ..Activity.this instead of getApplicationContext();
一般来说,使用 ..Activity.this 而不是 getApplicationContext();
Further reading: developer.android.com/reference/android/content/Context.html#getApplicationContext()
进一步阅读:developer.android.com/reference/android/content/Context.html#getApplicationContext()