Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions crates/tauri/mobile/android-codegen/TauriActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@ package {{package}}
import android.content.Intent
import android.content.res.Configuration
import app.tauri.plugin.PluginManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner

object TauriLifecycleObserver : DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
PluginManager.onResume()
}

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
PluginManager.onPause()
}

override fun onStop(owner: LifecycleOwner) {
super.onStop(owner)
PluginManager.onStop()
}
}

abstract class TauriActivity : WryActivity() {
override val handleBackNavigation: Boolean = false
Expand All @@ -47,6 +27,21 @@ abstract class TauriActivity : WryActivity() {
PluginManager.onRestart(this)
}

override fun onResume() {
super.onResume()
PluginManager.onResume(this)
}

override fun onPause() {
super.onPause()
PluginManager.onPause(this)
}

override fun onStop() {
super.onStop()
PluginManager.onStop(this)
}

override fun onDestroy() {
super.onDestroy()
PluginManager.onDestroy(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,24 @@ abstract class Plugin(private val activity: Activity) {
/**
* This event is called just before another activity comes into the foreground.
*/
open fun onPause(activity: AppCompatActivity) {}

/**
* This event is called just before another activity comes into the foreground.
*/
@Deprecated("use onPause(activity: AppCompatActivity) instead")
open fun onPause() {}

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

/**
* This event is called when the user returns to the activity. It is also called on cold starts.
*/
@Deprecated("use onResume(activity: AppCompatActivity) instead")
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).
Expand All @@ -90,6 +101,13 @@ abstract class Plugin(private val activity: Activity) {
* 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(activity: AppCompatActivity) {}

/**
* 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.
*/
@Deprecated("use onStop(activity: AppCompatActivity) instead")
open fun onStop() {}

/**
Expand All @@ -112,6 +130,21 @@ abstract class Plugin(private val activity: Activity) {
onRestart()
}

internal fun triggerOnPause(activity: AppCompatActivity) {
onPause(activity)
onPause()
}

internal fun triggerOnResume(activity: AppCompatActivity) {
onResume(activity)
onResume()
}

internal fun triggerOnStop(activity: AppCompatActivity) {
onStop(activity)
onStop()
}

/**
* This event is called when a configuration change occurs but the app does not recreate the activity.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ object PluginManager {
}
}

fun onPause() {
fun onPause(activity: AppCompatActivity) {
for (plugin in plugins.values) {
plugin.instance.onPause()
plugin.instance.triggerOnPause(activity)
}
}

fun onResume() {
fun onResume(activity: AppCompatActivity) {
for (plugin in plugins.values) {
plugin.instance.onResume()
plugin.instance.triggerOnResume(activity)
}
}

Expand All @@ -110,9 +110,9 @@ object PluginManager {
}
}

fun onStop() {
fun onStop(activity: AppCompatActivity) {
for (plugin in plugins.values) {
plugin.instance.onStop()
plugin.instance.triggerOnStop(activity)
}
}

Expand Down
Loading