Commit e9dc275f authored by Mygod's avatar Mygod

Ensure code only run when created

parent 3d079f29
...@@ -32,6 +32,7 @@ import android.view.MenuItem ...@@ -32,6 +32,7 @@ import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated
import androidx.preference.* import androidx.preference.*
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
...@@ -171,7 +172,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -171,7 +172,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
super.onAttach(context) super.onAttach(context)
receiver = context.listenForPackageChanges(false) { receiver = context.listenForPackageChanges(false) {
lifecycleScope.launch(Dispatchers.Main) { // wait until changes were flushed lifecycleScope.launch(Dispatchers.Main) { // wait until changes were flushed
lifecycleScope.launchWhenCreated { initPlugins() } whenCreated { initPlugins() }
} }
} }
} }
......
...@@ -30,12 +30,14 @@ import androidx.appcompat.widget.TooltipCompat ...@@ -30,12 +30,14 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe import androidx.lifecycle.observe
import androidx.lifecycle.whenStarted
import com.github.shadowsocks.MainActivity import com.github.shadowsocks.MainActivity
import com.github.shadowsocks.R import com.github.shadowsocks.R
import com.github.shadowsocks.bg.BaseService import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.net.HttpsTest import com.github.shadowsocks.net.HttpsTest
import com.google.android.material.bottomappbar.BottomAppBar import com.google.android.material.bottomappbar.BottomAppBar
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
...@@ -76,8 +78,8 @@ class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? ...@@ -76,8 +78,8 @@ class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet?
fun changeState(state: BaseService.State) { fun changeState(state: BaseService.State) {
val activity = context as MainActivity val activity = context as MainActivity
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launchWhenStarted { fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launch {
withContext(Dispatchers.Main) { what() } withContext(Dispatchers.Main) { activity.whenStarted { what() } }
} }
if ((state == BaseService.State.Connected).also { hideOnScroll = it }) { if ((state == BaseService.State.Connected).also { hideOnScroll = it }) {
postWhenStarted { performShow() } postWhenStarted { performShow() }
......
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 to comment