问题是如何启动适当的Activity上下文来获得Fragment Manager?从可组合对象和Fragment之间的互操作性的Angular 来看,这可能吗?
@Keep
class Card @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : FrameLayout( // or any other View you want
// don't forget to use context wrapper and to apply your own theme
ContextThemeWrapper(
context,
context.resources.newTheme().apply { applyStyle(R.style.FantasyTheme, true) }
),
attrs
), GamingHubView {
override fun initialize(data: Map<String, Any>?) {
// inflate a view or render views dynamically
// inflate(context, R.layout.view_card, this)
val transaction: FragmentTransaction =
(this.context as AppCompatActivity).supportFragmentManager.beginTransaction()
transaction.replace(
this.id,
BlankFragment.newInstance("", ""),
BlankFragment::class.simpleName
)
transaction.addToBackStack(null)
transaction.commit()
}
}
/**
* Get activity instance from desired context.
*/
fun getActivity(context: Context?): AppCompatActivity? {
if (context == null) return null
if (context is AppCompatActivity) return context
return if (context is ContextWrapper) getActivity(context.baseContext) else null
}
1条答案
按热度按时间gkn4icbw1#
我已经到处搜索了这个问题,但是我没有找到具体的答案。我猜你想做的是把compose和fragment结合起来。在我的例子中,我所做的是创建一个MyActivity,它有一个compose视图,像这样充满整个屏幕
然后在MyActivity onCreate方法中,示例化编写视图,如下所示
然后,在Activity中的任何地方,都可以访问
supportFragmentManager
,可以使用它来执行片段事务。