android - 如果 Android Activity/Application/Service 的上下文属于同一个应用程序,它们是同一个对象吗?

标签 android android-context

假设 Application 中的 getApplicationContext 和 AbstractThreadedSyncAdapter 中的 getContext 返回相同的对象是否安全?我们从 Activity 、服务中获取上下文的其他情况如何……都属于同一个应用程序。谢谢。

最佳答案

不,他们不是。这是要点:

Application – is a singleton instance running in your application process. It can be accessed via methods like getApplication() from an Activity or Service, and getApplicationContext() from any other object that inherits from Context. Regardless of where or how it is accessed, you will always receive the same instance from within your process.

Activity/Service – inherit from ContextWrapper which implements the same API, but proxies all of its method calls to a hidden internal Context instance, also known as its base context. Whenever the framework creates a new Activity or Service instance, it also creates a new ContextImpl instance to do all of the heavy lifting that either component will wrap. Each Activity or Service, and their corresponding base context, are unique per-instance.

这篇文章确实有助于清除它:https://possiblemobile.com/2013/06/context/

关于android - 如果 Android Activity/Application/Service 的上下文属于同一个应用程序,它们是同一个对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32257478/

相关文章:

android - 如何使用事件管理创建自定义日历?

php - 将帐户信息更新到数据库不起作用,PHP 错误 : "Call to a member function"

android - 来自不同上下文的 StartActivity

kotlin - 从 RecyclerView 适配器内部访问 AlarmManager

android - Activity 泄露了原本绑定(bind)的 ServiceConnection com.google.android.youtube.player.internal.r$e@43cb2348

android - 防止 ImageButton 进入后台

java - 如何隐藏底部导航栏?

android - AsyncTask 中的上下文

android - 如何从没有上下文的类中调用 getResources()?

android - 如何在 Android 设备上启动 wpa_cli?