简体   繁体   English

通过动态功能中的 ServiceLoader 启动片段 (Android)

[英]Launching fragment via ServiceLoader in Dynamic Feature (Android)

I'm trying to launch a fragment in my a dynamic feature module using a ServiceLoader as follows, but I get a crash the first time I try to launch my fragment.我正在尝试使用 ServiceLoader 在我的动态功能模块中启动一个片段,如下所示,但是我第一次尝试启动我的片段时遇到了崩溃。 After the initial crash, if I try to launch the fragment again, it loads fine.初次崩溃后,如果我再次尝试启动该片段,它会正常加载。

I am checking if the module is installed before trying to launch the fragment.在尝试启动片段之前,我正在检查模块是否已安装。 Why am I still getting this first-time crash?为什么我仍然遇到第一次崩溃? It's almost as if the serviceLoader isn't fully loaded unless I leave the app idle for another second.这几乎就像 serviceLoader 没有完全加载,除非我让应用程序再闲置一秒钟。


splitInstallManager = SplitInstallManagerFactory.create(this)

val request = SplitInstallRequest
    .newBuilder()
    .addModule("dynamicFragment")
    .build()

splitInstallManager.startInstall(request)
    .addOnSuccessListener { sessionId ->

    if (!splitInstallManager.installedModules.contains("dynamicFragment"))
        return@addOnSuccessListener

    findViewById<TextView>(R.id.installStatus).text = "Successfully installed dynamic module, sessionId=$sessionId"

    val launch = findViewById<Button>(R.id.launchFragmentBtn)
    launch.visibility = View.VISIBLE
    launch.setOnClickListener {

        val serviceLoader = ServiceLoader.load(
            DynamicFragmentContract.Provider::class.java,
                 DynamicFragmentContract.Provider::class.java.classLoader)

        val c: DynamicFragmentContract = serviceLoader.iterator().next().get()
        val fragment = c as Fragment                        
   
        supportFragmentManager
            .beginTransaction().add(R.id.fragmentContainer, f).commit()
2022-03-25 21:06:31.960 18125-18125/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp.dynamicfeaturemodulesapp, PID: 18125
    java.util.NoSuchElementException
        at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:366)
        at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:416)
        at java.util.ServiceLoader$1.next(ServiceLoader.java:494)
        at com.myapp.mylibrary.LibraryActivity.onCreate$lambda-3$lambda-1$lambda-0(LibraryActivity.kt:61)
        at com.myapp.mylibrary.LibraryActivity.$r8$lambda$_Qbmh4qCZoH4E5ov5s1Js7ZPauo(Unknown Source:0)
        at com.myapp.mylibrary.LibraryActivity$$ExternalSyntheticLambda1.onClick(Unknown Source:2)

Turns out splitInstallManager.addOnSuccessListener wasn't reliably reflecting the download/install status.原来splitInstallManager.addOnSuccessListener不能可靠地反映下载/安装状态。 After I changed it to use the SplitInstallStateUpdatedListener , things are working as expected.在我将其更改为使用SplitInstallStateUpdatedListener ,一切都按预期进行。

private val splitInstallStateUpdatedListener = SplitInstallStateUpdatedListener { state ->
        val names = state.moduleNames().joinToString(" - ")
        when (state.status()) {
            SplitInstallSessionStatus.DOWNLOADING -> {
                //  In order to see this, the application has to be uploaded to the Play Store.
                findViewById<TextView>(R.id.installStatus).text = "Downloading $names..."
            }
            SplitInstallSessionStatus.INSTALLED -> {
                findViewById<TextView>(R.id.installStatus).text =
                    "Successfully installed $names dynamic feature module"

                onSuccessfullyInstalled()
            }

            SplitInstallSessionStatus.INSTALLING -> {
                findViewById<TextView>(R.id.installStatus).text = "Installing $names..."
            }
            SplitInstallSessionStatus.FAILED -> {
                findViewById<TextView>(R.id.installStatus).text =
                    "Error: ${state.errorCode()} for module ${state.moduleNames()}"
            }
        }
    }

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

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