vitaviva / fragivity

Use Fragment like Activity
https://juejin.cn/post/6918693610359619592
MIT License
298 stars 40 forks source link

[IllegalStateException] Fragment xxx not associated with a fragment manager. #46

Closed JulyStar-Lv closed 3 years ago

JulyStar-Lv commented 3 years ago

0.2.9版本 fragment流程 A -> B , B ->C, C(popSelf) -> D, D(pop) 回到B, 再从B(pop) 回到A必现闪退 0.2.5版本没有这个问题,由于出现了issue #43 一样的crash,所以升级了版本

07-12 14:33:22.877  3072  3072 E AndroidRuntime: FATAL EXCEPTION: main
07-12 14:33:22.877  3072  3072 E AndroidRuntime: Process: cn.net.cloudthink.smartmirror, PID: 3072
07-12 14:33:22.877  3072  3072 E AndroidRuntime: java.lang.IllegalStateException: Fragment FamilyFragment{fc108d} (ce6e6caa-fe6c-4dae-a966-eabbe8954140) not associated with a fragment manager.
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at androidx.fragment.app.Fragment.getParentFragmentManager(Fragment.java:1040)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:109)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at com.github.fragivity.LifecycleKt.getNavigator(Lifecycle.kt:20)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at cn.com.broadlink.smartscreen.common.base.BaseFragment$onResume$2.onClick(BaseFragment.kt:90)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at cn.com.broadlink.library.base.ext.binding.ViewClickAdapterKt$clickWithTrigger$1.onClick(ViewClickAdapter.kt:94)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.view.View.performClick(View.java:5637)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.view.View$PerformClick.run(View.java:22445)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:755)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:95)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6121)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
07-12 14:33:22.877  3072  3072 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
qdsfdhvh commented 3 years ago

收到,尽快修复

qdsfdhvh commented 3 years ago

我粗略试了下没试出来,方便整理一个简单的demo吗

JulyStar-Lv commented 3 years ago

这两天找时间整理个demo出来。 找了个规避方法,使用下面后退按键方式代替navigator.pop(),就不会出现这个异常了

object NavigationUtils {
    fun clickBack(
        onSuccess: (() -> Unit)? = null,
        onFailed: ((e: Exception) -> Unit)? = null
    ) {
        Thread {
            try {
                val instrumentation = Instrumentation()
                instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK)
                onSuccess?.invoke()
            } catch (e: Exception) {
                e.printStackTrace()
                onFailed?.invoke(e)
            }
        }.start()
    }
}
JulyStar-Lv commented 3 years ago

下载地址, https://home.kbhy.online:10003/d/s/631664212403298306/VedMf523wD9RYHgAAGYm-7ZKr4fB0Vev-AyDgBlYfxAg_ (复制,粘贴到浏览器能打开,直接打开链接的话,打不开)

lib_base模块的build.gradle 将'com.github.vitaviva.fragivity:core:0.2.9'改成'com.github.vitaviva.fragivity:core:0.2.5', 就没问题

还是这个流程 fragment流程 A -> B , B ->C, C(popSelf) -> D, D(pop) 回到B, 再从B(pop) 回到A必现闪退

我看了下 应该是0.2.9版本 D(pop) 回到B时,B没有走onresume流程导致的

qdsfdhvh commented 3 years ago

找到原因了,库里为了让Fragment走onStart/onStop用了些偏门,所以需要在Activity.onCreate中添加proxyFragmentFactory方法。

class HasLauncherActivity : BaseActivity<EmptyViewModel, ActivityHasLauncherBinding>() {
     ...
    override fun onCreate(savedInstanceState: Bundle?) {
        proxyFragmentFactory()
        super.onCreate(savedInstanceState)
    }
    ...
}
JulyStar-Lv commented 3 years ago

好的,收到,谢谢!👍