Skip to content

Commit 68cb318

Browse files
authored
feat(core): add stop, restart, destroy and configuration changed Android hooks (#14328)
* feat(core): add pause, destroy and configuration changed Android hooks * Apply suggestions from code review
1 parent 3397fd9 commit 68cb318

6 files changed

Lines changed: 77 additions & 4 deletions

File tree

.changes/extended-hooks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor:feat
3+
---
4+
5+
Added `onStop`, `onDestroy`, `onRestart`, `onConfigurationChanged` Android plugin hooks.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ wry = { version = "0.53.4", default-features = false, features = [
2323
"os-webview",
2424
"linux-body",
2525
] }
26-
tao = { version = "0.34.2", default-features = false, features = ["rwh_06"] }
26+
tao = { version = "0.34.4", default-features = false, features = ["rwh_06"] }
2727
tauri-runtime = { version = "2.8.0", path = "../tauri-runtime" }
2828
tauri-utils = { version = "2.7.0", path = "../tauri-utils" }
2929
raw-window-handle = "0.6"

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
package {{package}}
88

9-
import android.os.Bundle
109
import android.content.Intent
10+
import android.content.res.Configuration
1111
import app.tauri.plugin.PluginManager
1212

1313
abstract class TauriActivity : WryActivity() {
@@ -28,4 +28,24 @@ abstract class TauriActivity : WryActivity() {
2828
super.onPause()
2929
pluginManager.onPause()
3030
}
31+
32+
override fun onRestart() {
33+
super.onRestart()
34+
pluginManager.onRestart()
35+
}
36+
37+
override fun onStop() {
38+
super.onStop()
39+
pluginManager.onStop()
40+
}
41+
42+
override fun onDestroy() {
43+
super.onDestroy()
44+
pluginManager.onDestroy()
45+
}
46+
47+
override fun onConfigurationChanged(newConfig: Configuration) {
48+
super.onConfigurationChanged(newConfig)
49+
pluginManager.onConfigurationChanged(newConfig)
50+
}
3151
}

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

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

77
import android.app.Activity
8+
import android.content.res.Configuration
89
import android.content.Intent
910
import android.content.pm.PackageManager
1011
import android.net.Uri
@@ -70,6 +71,28 @@ abstract class Plugin(private val activity: Activity) {
7071
*/
7172
open fun onResume() {}
7273

74+
/**
75+
* This event is called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it).
76+
* It will be followed by onStart() and then onResume().
77+
*/
78+
open fun onRestart() {}
79+
80+
/**
81+
* This event is called when the app is no longer visible to the user.
82+
* You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
83+
*/
84+
open fun onStop() {}
85+
86+
/**
87+
* This event is called before the activity is destroyed.
88+
*/
89+
open fun onDestroy() {}
90+
91+
/**
92+
* This event is called when a configuration change occurs but the app does not recreate the activity.
93+
*/
94+
open fun onConfigurationChanged(newConfig: Configuration) {}
95+
7396
/**
7497
* Start activity for result with the provided Intent and resolve calling the provided callback method name.
7598
*

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

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

77
import android.app.PendingIntent
8+
import android.content.res.Configuration
89
import android.content.Context
910
import android.content.Intent
1011
import android.webkit.WebView
@@ -98,6 +99,30 @@ class PluginManager(val activity: AppCompatActivity) {
9899
}
99100
}
100101

102+
fun onRestart() {
103+
for (plugin in plugins.values) {
104+
plugin.instance.onRestart()
105+
}
106+
}
107+
108+
fun onStop() {
109+
for (plugin in plugins.values) {
110+
plugin.instance.onStop()
111+
}
112+
}
113+
114+
fun onDestroy() {
115+
for (plugin in plugins.values) {
116+
plugin.instance.onDestroy()
117+
}
118+
}
119+
120+
fun onConfigurationChanged(newConfig: Configuration) {
121+
for (plugin in plugins.values) {
122+
plugin.instance.onConfigurationChanged(newConfig)
123+
}
124+
}
125+
101126
fun startActivityForResult(intent: Intent, callback: ActivityResultCallback) {
102127
startActivityForResultCallback = callback
103128
startActivityForResultLauncher.launch(intent)

0 commit comments

Comments
 (0)