Skip to content

Commit b8cb71a

Browse files
committed
fix: intent handling using listener and singleTop
1 parent 91a9b18 commit b8cb71a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<activity
3434
android:name=".MainActivity"
3535
android:exported="true"
36+
android:launchMode="singleInstance"
3637
android:theme="@style/Theme.Qbit">
3738
<intent-filter>
3839
<action android:name="android.intent.action.MAIN" />

app/src/main/kotlin/dev/yashgarg/qbit/ui/server/ServerFragment.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.os.Bundle
77
import android.util.Log
88
import android.view.View
99
import android.widget.Toast
10+
import androidx.core.os.bundleOf
1011
import androidx.fragment.app.Fragment
1112
import androidx.fragment.app.setFragmentResultListener
1213
import androidx.fragment.app.viewModels
@@ -44,6 +45,11 @@ class ServerFragment : Fragment(R.layout.server_fragment) {
4445

4546
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
4647
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)
48+
49+
activity?.addOnNewIntentListener {
50+
val bundle = bundleOf(MainActivity.TORRENT_INTENT_KEY to it?.data.toString())
51+
handleAddIntent(bundle)
52+
}
4753
}
4854

4955
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -108,9 +114,9 @@ class ServerFragment : Fragment(R.layout.server_fragment) {
108114
}
109115
}
110116

111-
private fun handleAddIntent() {
112-
val uri: String? = arguments?.getString(MainActivity.TORRENT_INTENT_KEY)
113-
arguments?.clear()
117+
private fun handleAddIntent(bundle: Bundle?) {
118+
val uri: String? = (bundle ?: arguments)?.getString(MainActivity.TORRENT_INTENT_KEY)
119+
(bundle ?: arguments)?.clear()
114120
if (!uri.isNullOrEmpty()) {
115121
when {
116122
linkValidator.isValid(uri) -> viewModel.addTorrentUrl(uri)
@@ -207,7 +213,7 @@ class ServerFragment : Fragment(R.layout.server_fragment) {
207213

208214
viewModel.intent
209215
.flowWithLifecycle(viewLifecycleOwner.lifecycle)
210-
.onEach { handleAddIntent() }
216+
.onEach { handleAddIntent(null) }
211217
.launchIn(viewLifecycleOwner.lifecycleScope)
212218
}
213219

0 commit comments

Comments
 (0)