Commit e7cc5313 by huangzhicong

修复可能在lifecycleScope未执行完抛出的异常导致崩溃的问题

parent 51cb0ccd
...@@ -17,6 +17,7 @@ import kotlinx.coroutines.Dispatchers ...@@ -17,6 +17,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class StartFragment : Fragment() { class StartFragment : Fragment() {
companion object { companion object {
...@@ -46,18 +47,25 @@ class StartFragment : Fragment() { ...@@ -46,18 +47,25 @@ class StartFragment : Fragment() {
button.setOnClickListener { button.setOnClickListener {
val url = urlEditText.text.trim().toString() val url = urlEditText.text.trim().toString()
if (url.isNotEmpty()) { if (url.isEmpty()) {
lifecycleScope.launch(Dispatchers.IO) { findNavController().navigate(
val storageKey = stringPreferencesKey(STORAGE_URL) R.id.action_startFragment_to_mainFragment,
context?.dataStore?.edit { MainFragment.createStartBundle(url)
it[storageKey] = url )
} return@setOnClickListener
}
lifecycleScope.launch(Dispatchers.IO) {
val storageKey = stringPreferencesKey(STORAGE_URL)
context?.dataStore?.edit {
it[storageKey] = url
}
withContext(Dispatchers.Main) {
findNavController().navigate(
R.id.action_startFragment_to_mainFragment,
MainFragment.createStartBundle(url)
)
} }
} }
findNavController().navigate(
R.id.action_startFragment_to_mainFragment,
MainFragment.createStartBundle(url)
)
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment