Android 使用组件名称来启动 Activity
开始另一项活动的方法有很多种。 大多数重载方法都需要您传递上下文。
启动活动时
public Intent setComponent (ComponentName component)
但是,当使用 componentName 使用ComponentName 的构造函数
ComponentName(String pkg, String cls)
,您在上面看到,我可以在不使用任何 Context 参数的情况下启动活动,
但它必须在内部以某种方式使用一些“上下文”,对吗?如果是这样,是在什么背景下?应用一还是活动一? 这是否意味着每次我使用这两种方法(上面)时,我不需要担心内存泄漏,因为我没有传递任何上下文?
谢谢
There are many ways to start another activity.
The most of the overloading methods requires you to pass a context.
But when using componentName to launch an activity using
public Intent setComponent (ComponentName component)
and this constructor for ComponentName
ComponentName(String pkg, String cls)
You see above, I am able to launch an activity WITHOUT using any Context argument
But it must use some "context" somehow internally, am I right? If so, which context? Application one or the activity one?
Does this mean that every time I use this two methods (above), I do not need to worry about memory leak becuase I am not passing any context around??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这两种情况下,您都不必担心内存泄漏,但最好密切关注传递 Context 对象的位置。当您使用
Intent(Context, Class)
构造函数或setClass(Context, Class)
方法。它们只是方便的方法。You don't have to worry about memory leaks in either case, but it's good that you're keeping an eye on where you're passing Context objects. Intent simply uses the Context parameter to look up your package name when you use the
Intent(Context, Class)
constructor orsetClass(Context, Class)
method. They're just convenience methods.阿达普的答案是正确的(他在我发帖之前就得到了答案)。
只是为了扩展它,这是
Intent(Context packageContext, Class> cls)
构造函数的源代码......这是
ComponentName(Context pkg) 的源代码, Class cls)
构造函数正如 adamp 所暗示的那样,采用
Context
的Intent
方法是仅使用它来创建>ComponentName
其中反过来,仅处理String
类型(mPackage
和mClass
)。Intent
和ComponentName
都不保存对Context
的引用。adamp's answer is correct (he got to it before I could post).
Just to expand on it this is the source for the
Intent(Context packageContext, Class<?> cls)
constructor......and this is the source for
ComponentName(Context pkg, Class<?> cls)
constructorAs adamp implies, the
Intent
methods that take aContext
are convenience methods that only use it to create theComponentName
which in turn only deals inString
types (mPackage
andmClass
). Neither theIntent
nor theComponentName
hold a reference to theContext
.startActivity()
不需要上下文作为参数;它是已经从 Context 派生(或实现)的类中的方法。也就是说,如果您没有调用 startActivity() 的上下文,则无法调用它。startActivity()
does not require a context as a parameter; it's a method within a class which is already derived from (or implements) Context. That is -- you cannot callstartActivity()
if you do not have a Context from which to call it.也许我没明白你的问题。但是在定义意图时不使用上下文。您可以使用上下文来使用意图来调用组件。例如,您使用:
但通常您在扩展 Context 的活动和服务中调用这些方法。因此,您只需使用:
Maybe I did not understand your question. But you do not use context when you defining intents. You use context to call components using intents. For instance, you use:
But usually you call these methods inside your Activities and Services, that extends Context. Thus, you simply use: