第一次打开fragment时ComposeView非常慢

Net*_*hak 11 android android-fragments kotlin android-jetpack-compose

我将 compose 添加到现有项目中。我用compose重写了一个fragment ui,当我启动该fragment时,需要很长时间才能启动。

从另一个片段添加一个片段:

 val fragment = FragmentWithComposeUi()
 requireActivity().addFragment(fragment, R.id.fragment_container, "FragmentWithComposeUi")
Run Code Online (Sandbox Code Playgroud)

addFragment()添加片段的功能。

fun FragmentActivity.addFragment(fragment: Fragment, container: Int, tag:String) {
    val currentFragment = supportFragmentManager.findFragmentByTag(tag)
    if (currentFragment == null) {
        supportFragmentManager.beginTransaction()
            .setReorderingAllowed(true)
            .add(container, fragment, tag)
            .addToBackStack(tag)
            .commit()
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentWithComposeUi 类:

class FragmentWithComposeUi: Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply{
            setContent {
               //some ui
            }
        }
    }

Run Code Online (Sandbox Code Playgroud)

构建.gradle

我也尝试使用新的 compose 版本 1.3.0-alpha01,但没有帮助。

buildscript {
    ext {
        compose_version = '1.1.1'
        compose_compiler_version = '1.2.0'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
      //
     }
}


plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}



task clean(type: Delete) {
    delete rootProject.buildDir
}

Run Code Online (Sandbox Code Playgroud)

构建.gradle/应用程序

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.e...."
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    viewBinding {
        enabled = true
    }

    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
        resources.excludes.add("META-INF/*")

    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_compiler_version
    }

}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation("androidx.multidex:multidex:2.0.1")
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-reactivestreams-ktx:2.5.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    //compose
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

...
}
Run Code Online (Sandbox Code Playgroud)

有什么原因会发生这种情况吗?谢谢 !