Skip to content

Commit b89de9f

Browse files
feat(android): Add onResume and onPause to android plugins (#8092)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 0601d5d commit b89de9f

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.changes/android-onresume.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:enhance'
3+
---
4+
5+
Add support for onResume and onPause events in android plugins.

core/tauri/mobile/android-codegen/TauriActivity.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ import app.tauri.plugin.PluginManager
1313
abstract class TauriActivity : WryActivity() {
1414
var pluginManager: PluginManager = PluginManager(this)
1515

16-
override fun onCreate(savedInstanceState: Bundle?) {
17-
super.onCreate(savedInstanceState)
18-
if (intent != null) {
19-
pluginManager.onNewIntent(intent)
20-
}
21-
}
22-
2316
override fun onNewIntent(intent: Intent) {
2417
super.onNewIntent(intent)
2518
pluginManager.onNewIntent(intent)
2619
}
20+
21+
override fun onResume() {
22+
super.onResume()
23+
pluginManager.onResume()
24+
}
25+
26+
override fun onPause() {
27+
super.onPause()
28+
pluginManager.onPause()
29+
}
2730
}

core/tauri/mobile/android/src/main/java/app/tauri/plugin/Plugin.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.app.Activity
88
import android.content.Intent
99
import android.content.pm.PackageManager
1010
import android.net.Uri
11+
import android.os.Bundle
1112
import android.webkit.WebView
1213
import androidx.core.app.ActivityCompat
1314
import app.tauri.FsUtils
@@ -59,6 +60,17 @@ abstract class Plugin(private val activity: Activity) {
5960
*/
6061
open fun onNewIntent(intent: Intent) {}
6162

63+
64+
/**
65+
* This event is called just before another activity comes into the foreground.
66+
*/
67+
open fun onPause() {}
68+
69+
/**
70+
* This event is called when the user returns to the activity. It is also called on cold starts.
71+
*/
72+
open fun onResume() {}
73+
6274
/**
6375
* Start activity for result with the provided Intent and resolve calling the provided callback method name.
6476
*

core/tauri/mobile/android/src/main/java/app/tauri/plugin/PluginManager.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package app.tauri.plugin
66

77
import android.content.Context
88
import android.content.Intent
9+
import android.os.Bundle
910
import android.webkit.WebView
1011
import androidx.activity.result.ActivityResult
1112
import androidx.activity.result.ActivityResultLauncher
@@ -71,6 +72,18 @@ class PluginManager(val activity: AppCompatActivity) {
7172
}
7273
}
7374

75+
fun onPause() {
76+
for (plugin in plugins.values) {
77+
plugin.instance.onPause()
78+
}
79+
}
80+
81+
fun onResume() {
82+
for (plugin in plugins.values) {
83+
plugin.instance.onResume()
84+
}
85+
}
86+
7487
fun startActivityForResult(intent: Intent, callback: ActivityResultCallback) {
7588
startActivityForResultCallback = callback
7689
startActivityForResultLauncher.launch(intent)

0 commit comments

Comments
 (0)