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
This commit is contained in:
Lucas Fernandes Nogueira
2025-10-20 08:49:26 -03:00
committed by GitHub
parent 3397fd9bfe
commit 68cb318979
6 changed files with 77 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": minor:feat
---
Added `onStop`, `onDestroy`, `onRestart`, `onConfigurationChanged` Android plugin hooks.

4
Cargo.lock generated
View File

@@ -8411,9 +8411,9 @@ dependencies = [
[[package]]
name = "tao"
version = "0.34.2"
version = "0.34.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4daa814018fecdfb977b59a094df4bd43b42e8e21f88fddfc05807e6f46efaaf"
checksum = "6121216ff67fe4bcfe64508ea1700bc15f74937d835a07b4a209cc00a8926a84"
dependencies = [
"bitflags 2.7.0",
"block2 0.6.0",

View File

@@ -23,7 +23,7 @@ wry = { version = "0.53.4", default-features = false, features = [
"os-webview",
"linux-body",
] }
tao = { version = "0.34.2", default-features = false, features = ["rwh_06"] }
tao = { version = "0.34.4", default-features = false, features = ["rwh_06"] }
tauri-runtime = { version = "2.8.0", path = "../tauri-runtime" }
tauri-utils = { version = "2.7.0", path = "../tauri-utils" }
raw-window-handle = "0.6"

View File

@@ -6,8 +6,8 @@
package {{package}}
import android.os.Bundle
import android.content.Intent
import android.content.res.Configuration
import app.tauri.plugin.PluginManager
abstract class TauriActivity : WryActivity() {
@@ -28,4 +28,24 @@ abstract class TauriActivity : WryActivity() {
super.onPause()
pluginManager.onPause()
}
override fun onRestart() {
super.onRestart()
pluginManager.onRestart()
}
override fun onStop() {
super.onStop()
pluginManager.onStop()
}
override fun onDestroy() {
super.onDestroy()
pluginManager.onDestroy()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
pluginManager.onConfigurationChanged(newConfig)
}
}

View File

@@ -5,6 +5,7 @@
package app.tauri.plugin
import android.app.Activity
import android.content.res.Configuration
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
@@ -70,6 +71,28 @@ abstract class Plugin(private val activity: Activity) {
*/
open fun onResume() {}
/**
* This event is called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it).
* It will be followed by onStart() and then onResume().
*/
open fun onRestart() {}
/**
* This event is called when the app is no longer visible to the user.
* You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
*/
open fun onStop() {}
/**
* This event is called before the activity is destroyed.
*/
open fun onDestroy() {}
/**
* This event is called when a configuration change occurs but the app does not recreate the activity.
*/
open fun onConfigurationChanged(newConfig: Configuration) {}
/**
* Start activity for result with the provided Intent and resolve calling the provided callback method name.
*

View File

@@ -5,6 +5,7 @@
package app.tauri.plugin
import android.app.PendingIntent
import android.content.res.Configuration
import android.content.Context
import android.content.Intent
import android.webkit.WebView
@@ -98,6 +99,30 @@ class PluginManager(val activity: AppCompatActivity) {
}
}
fun onRestart() {
for (plugin in plugins.values) {
plugin.instance.onRestart()
}
}
fun onStop() {
for (plugin in plugins.values) {
plugin.instance.onStop()
}
}
fun onDestroy() {
for (plugin in plugins.values) {
plugin.instance.onDestroy()
}
}
fun onConfigurationChanged(newConfig: Configuration) {
for (plugin in plugins.values) {
plugin.instance.onConfigurationChanged(newConfig)
}
}
fun startActivityForResult(intent: Intent, callback: ActivityResultCallback) {
startActivityForResultCallback = callback
startActivityForResultLauncher.launch(intent)