首页 > 解决方案 > 片段 - 在创建片段之前取消片段事务

问题描述

MyFragmentlayout_my_fragment.xml一个 WebView 小部件。MyFragment不会膨胀视图本身,它使用继承:

MyFragment <- ParentFragment <- GrandParentFragment (layout is inflated here)

目标是在扩展其布局之前检查手机是否启用了 WebView (这会使应用程序崩溃)。

到目前为止我的解决方案:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    checkWebviewIsEnabled()

    return super.onCreateView(inflater, container, savedInstanceState)
}

override fun checkWebviewIsEnabled(): Boolean {
    if(!isWebviewEnabled(requireActivity())) {
        findNavController().popBackStack()
    }

    return true
}

findNavController().popBackStack(),这是退出片段的正常方式,被调用但不做任何事情并且return true被调用。我怀疑这是因为这种方式仅在创建片段后才有效。

那么如何在扩展其布局之前取消片段创建?

标签: androidkotlinandroid-fragments

解决方案


推荐阅读