bytedance / scene

Android Single Activity Framework compatible with Fragment.
Apache License 2.0
2.08k stars 198 forks source link

绑定到Fragment时onViewCreated生命周期 #63

Closed Iridescentangle closed 2 years ago

Iridescentangle commented 2 years ago
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  super.onViewCreated(view, savedInstanceState)
  if(delegate == null) {
    delegate = GroupSceneCompatUtility.setupWithFragment(this, MyScene::class.java, viewId).build()
  }
}

原本首页采用的是BottomNavigationView+Fragment,切换时hide->show的管理逻辑,这种情况下是正常的;

但是由于将其中一个Fragment改成了FlutterFragment(也是够缝合的),出现了[Fragment切换正常,但跳转新页面再返回,FlutterFragment就会置顶展示,而且底部导航错乱]的问题,所以考虑改动原有管理逻辑,现在改为replace,结果遇到绑定了Scene的Fragment,会再走一次onViewCreated(onCreateView没有走),这里加了空值判断,只是不会再抛出异常,但不会再展示了.请问这种情况下应该怎么做才能让Scene正常展示呢?

qii commented 2 years ago

我感觉这里的问题是,Fragment 的 replace 重新创建了 View,而之前绑的 Scene 还在上次 Fragment 的 View 里面。

你找到 delegate 的 GroupScene,getView 拿到 View,移除 View 的 parent,塞入到你新创建的 Fragment 的 View 的 viewId 里面

大致的逻辑是这样

if(delegate == null) { delegate = GroupSceneCompatUtility.setupWithFragment(this, MyScene::class.java, viewId).build() }else{ val groupSceneView = delegate.getGroupScene().getView() (groupSceneView.getParent() as ViewGroup).remove(groupSceneView) getView().findViewById(viewId).addView(groupSceneView) }

qii commented 2 years ago

等等,好像不对。。。你的 replace 为啥不会走 onCreateView?

你之前会遇到的抛出的异常是什么,贴来看看

Iridescentangle commented 2 years ago

对的没错...抛的异常就是tag duplicated

 static void checkDuplicateTag(@NonNull Fragment fragment, @NonNull String tag) {
        if (CHECK_DUPLICATE_TAG_MAP.get(fragment) != null && CHECK_DUPLICATE_TAG_MAP.get(fragment).contains(tag)) {
            throw new IllegalArgumentException("tag duplicate, use another tag when invoke setupWithActivity for the second time in same Fragment");
        } else {
            HashSet<String> set = CHECK_DUPLICATE_TAG_MAP.get(fragment);
            if (set == null) {
                set = new HashSet<>();
                CHECK_DUPLICATE_TAG_MAP.put(fragment, set);
            }
            set.add(tag);
        }
    }
Iridescentangle commented 2 years ago

仔细想了一下,我现在首页五个分页 一个Flutter实现 一个原生Fragment 剩下三个都是Scene绑定的Fragment,想来想去不如直接都迁移到Scene...并且在尝试这么做之后我发现BottomNavigationViewScene的切换并不会有Fragment replace类似的问题

qii commented 2 years ago

Orz 那我先关了这个issue