Skip to content

Commit d5fc83d

Browse files
authored
Fix: cancel coroutine when activity gets destroyed (duckduckgo#699)
* Cancel coroutine running on activity context when activity gets stopped. * remove fragment reference when activity gets destroyed.
1 parent 82362dc commit d5fc83d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import kotlin.coroutines.CoroutineContext
5656
class BrowserActivity : DuckDuckGoActivity(), CoroutineScope {
5757

5858
override val coroutineContext: CoroutineContext
59-
get() = SupervisorJob() + Dispatchers.Main
59+
get() = job + Dispatchers.Main
6060

6161
@Inject
6262
lateinit var clearPersonalDataAction: ClearPersonalDataAction
@@ -70,6 +70,8 @@ class BrowserActivity : DuckDuckGoActivity(), CoroutineScope {
7070
@Inject
7171
lateinit var playStoreUtils: PlayStoreUtils
7272

73+
private val job = SupervisorJob()
74+
7375
private var currentTab: BrowserTabFragment? = null
7476

7577
private val viewModel: BrowserViewModel by bindViewModel()
@@ -101,6 +103,16 @@ class BrowserActivity : DuckDuckGoActivity(), CoroutineScope {
101103
viewModel.awaitClearDataFinishedNotification()
102104
}
103105

106+
override fun onStop() {
107+
job.cancel()
108+
super.onStop()
109+
}
110+
111+
override fun onDestroy() {
112+
currentTab = null
113+
super.onDestroy()
114+
}
115+
104116
override fun onNewIntent(intent: Intent?) {
105117
super.onNewIntent(intent)
106118
Timber.i("onNewIntent: $intent")

0 commit comments

Comments
 (0)