Skip to content

Commit

Permalink
feat(android): Add onResume and onPause to android plugins (tauri-app…
Browse files Browse the repository at this point in the history
…s#8092)

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
FabianLars and lucasfernog authored Oct 28, 2023
1 parent 0601d5d commit b89de9f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/android-onresume.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'patch:enhance'
---

Add support for onResume and onPause events in android plugins.
17 changes: 10 additions & 7 deletions core/tauri/mobile/android-codegen/TauriActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import app.tauri.plugin.PluginManager
abstract class TauriActivity : WryActivity() {
var pluginManager: PluginManager = PluginManager(this)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent != null) {
pluginManager.onNewIntent(intent)
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
pluginManager.onNewIntent(intent)
}

override fun onResume() {
super.onResume()
pluginManager.onResume()
}

override fun onPause() {
super.onPause()
pluginManager.onPause()
}
}
12 changes: 12 additions & 0 deletions core/tauri/mobile/android/src/main/java/app/tauri/plugin/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.webkit.WebView
import androidx.core.app.ActivityCompat
import app.tauri.FsUtils
Expand Down Expand Up @@ -59,6 +60,17 @@ abstract class Plugin(private val activity: Activity) {
*/
open fun onNewIntent(intent: Intent) {}


/**
* This event is called just before another activity comes into the foreground.
*/
open fun onPause() {}

/**
* This event is called when the user returns to the activity. It is also called on cold starts.
*/
open fun onResume() {}

/**
* Start activity for result with the provided Intent and resolve calling the provided callback method name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package app.tauri.plugin

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
Expand Down Expand Up @@ -71,6 +72,18 @@ class PluginManager(val activity: AppCompatActivity) {
}
}

fun onPause() {
for (plugin in plugins.values) {
plugin.instance.onPause()
}
}

fun onResume() {
for (plugin in plugins.values) {
plugin.instance.onResume()
}
}

fun startActivityForResult(intent: Intent, callback: ActivityResultCallback) {
startActivityForResultCallback = callback
startActivityForResultLauncher.launch(intent)
Expand Down

0 comments on commit b89de9f

Please sign in to comment.