简体   繁体   English

从生命周期中获取活动上下文 object

[英]Get Activity Context from Lifecycle object

I have made my component Lifecycle-aware.我已经让我的组件具有生命周期意识。 I want to use activityContext in that component.我想在该组件中使用 activityContext 。

My question is: is there any way to use activityContext from the Lifecycle object?我的问题是:有什么方法可以使用Lifecycle object 中的 activityContext 吗?

There is not a way to do this - among other reasons, the LifeCycle object might not be backed by an Activity , or even anything attached to an Activity (for example, ProcessLifecycleOwner , and components depending on Lifecycle s should be agnostic of what that lifecycle is.没有办法做到这一点 - 除其他原因外, LifeCycle object 可能不受Activity支持,甚至任何附加到Activity的东西(例如, ProcessLifecycleOwner和依赖于Lifecycle的组件应该不知道该生命周期的内容是。

Instead, you should have the code creating your component pass it an Activity context.相反,您应该让创建组件的代码将其传递给Activity上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM