launch { whenStarted {} } 和 launchWhenStarted {} 完全一样吗?

Is launch { whenStarted {} } and launchWhenStarted {} exactly the same?

下面是不是一模一样?

lifecycleScope.launch {
    whenStarted { 
        // Do something
    }
}

lifecycleScope.launchWhenStarted {
    // Do something
}

或者它们确实有一些不同的目的,因此提供了两种 API?

lifecycleScope.launchWhenStarted {
    // Do something
}

只是

的简写
lifecycleScope.launch {
    whenStarted { 
        // Do something
    }
}

但是你可以做的是

lifecycleScope.launch {
    //do something here in general
    whenStarted { 
        // Do something onStart
    }
    whenCreated {
        // Do something onCreate
    }
    whenResumed {
        // Do something onResume
    }
}

一天结束的方法: launchWhenStarted& launchWhenResumed& launchWhenCreated 都在砧板上。以后会根据Android自己删除。所以我会避免使用它们,即使它可以为您节省一两行代码。

来源: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleCoroutineScope