Skip to content

Commit e9dc275

Browse files
committed
Ensure code only run when created
1 parent 3d079f2 commit e9dc275

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

mobile/src/main/java/com/github/shadowsocks/ProfileConfigFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import android.view.MenuItem
3232
import android.view.View
3333
import androidx.appcompat.app.AlertDialog
3434
import androidx.lifecycle.lifecycleScope
35+
import androidx.lifecycle.whenCreated
3536
import androidx.preference.*
3637
import com.github.shadowsocks.database.Profile
3738
import com.github.shadowsocks.database.ProfileManager
@@ -171,7 +172,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
171172
super.onAttach(context)
172173
receiver = context.listenForPackageChanges(false) {
173174
lifecycleScope.launch(Dispatchers.Main) { // wait until changes were flushed
174-
lifecycleScope.launchWhenCreated { initPlugins() }
175+
whenCreated { initPlugins() }
175176
}
176177
}
177178
}

mobile/src/main/java/com/github/shadowsocks/widget/StatsBar.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ import androidx.appcompat.widget.TooltipCompat
3030
import androidx.coordinatorlayout.widget.CoordinatorLayout
3131
import androidx.lifecycle.lifecycleScope
3232
import androidx.lifecycle.observe
33+
import androidx.lifecycle.whenStarted
3334
import com.github.shadowsocks.MainActivity
3435
import com.github.shadowsocks.R
3536
import com.github.shadowsocks.bg.BaseService
3637
import com.github.shadowsocks.net.HttpsTest
3738
import com.google.android.material.bottomappbar.BottomAppBar
3839
import kotlinx.coroutines.Dispatchers
40+
import kotlinx.coroutines.launch
3941
import kotlinx.coroutines.withContext
4042

4143
class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
@@ -76,8 +78,8 @@ class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet?
7678

7779
fun changeState(state: BaseService.State) {
7880
val activity = context as MainActivity
79-
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launchWhenStarted {
80-
withContext(Dispatchers.Main) { what() }
81+
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launch {
82+
withContext(Dispatchers.Main) { activity.whenStarted { what() } }
8183
}
8284
if ((state == BaseService.State.Connected).also { hideOnScroll = it }) {
8385
postWhenStarted { performShow() }

0 commit comments

Comments
 (0)