为什么在创建 Intent 实例时传递 'this'?
Why is 'this' passed while creating an instance of Intent?
我是 android 应用程序开发的新手。
我正在尝试了解其意图及其用途。
我的问题是,在启动另一个 activity 时,为什么 'this' 关键字作为意图的上下文参数传递?
Intent foo = new Intent(this, viewContacts.class);
我知道任何 activity 扩展了上下文 class,但为什么我们要传递 activity 上下文 而不是应用程序上下文?
我的观点-
当另一个 activity 启动时,当前 activity 将被销毁,但其上下文将传递给另一个。参考 this 文章,它说
The most obvious way of avoiding context related memory leak is to avoid escaping the context outside of its own scope.
那么我们不是将当前 activity 的上下文传递给第一个超出范围的另一个上下文吗?
不就是内存泄漏的例子吗?
why is it that we are passing the activity context and not the application context?
两者都可以在这里工作。 this
比 getApplicationContext()
.
打字更少,执行速度更快
When another activity starts the current activity will get destroyed but its context will passed to the other one.
您假设 Intent
持有此 Context
。它没有。
So aren't we passing the context of current activity to another one where the first one goes out of scope?
没有
Intent
可以是隐式的也可以是显式的。显式 Intent
是附加了 ComponentName
的应用程序,标识特定的应用程序(通过包名称)和 Java class(通过完全限定的 class 名称) 此 Intent
所针对的组件。提供 Context
和 Class
对象的双参数构造函数用于构建 ComponentName
。在构造函数工作完成后,Intent
和 ComponentName
都不会保留 Context
。
我是 android 应用程序开发的新手。 我正在尝试了解其意图及其用途。
我的问题是,在启动另一个 activity 时,为什么 'this' 关键字作为意图的上下文参数传递?
Intent foo = new Intent(this, viewContacts.class);
我知道任何 activity 扩展了上下文 class,但为什么我们要传递 activity 上下文 而不是应用程序上下文?
我的观点-
当另一个 activity 启动时,当前 activity 将被销毁,但其上下文将传递给另一个。参考 this 文章,它说
The most obvious way of avoiding context related memory leak is to avoid escaping the context outside of its own scope.
那么我们不是将当前 activity 的上下文传递给第一个超出范围的另一个上下文吗? 不就是内存泄漏的例子吗?
why is it that we are passing the activity context and not the application context?
两者都可以在这里工作。 this
比 getApplicationContext()
.
When another activity starts the current activity will get destroyed but its context will passed to the other one.
您假设 Intent
持有此 Context
。它没有。
So aren't we passing the context of current activity to another one where the first one goes out of scope?
没有
Intent
可以是隐式的也可以是显式的。显式 Intent
是附加了 ComponentName
的应用程序,标识特定的应用程序(通过包名称)和 Java class(通过完全限定的 class 名称) 此 Intent
所针对的组件。提供 Context
和 Class
对象的双参数构造函数用于构建 ComponentName
。在构造函数工作完成后,Intent
和 ComponentName
都不会保留 Context
。