Skip to content
Merged
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
17 changes: 16 additions & 1 deletion .changes/android-activity-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@
"wry": minor
---

Updated Android lifecycle JNI calls to pass the `WryActivity` instance for `start`, `resume`, `pause`, and `stop`, allowing Tao to emit window-specific lifecycle events.
Updated Android lifecycle JNI calls in `WryActivity` for Tao 0.36's renames:

> - `create` to `onFirstActivityCreate`
> - `onActivityCreate` to `onCreate`
> - `start` to `onStart`
> - `resume` to `onResume`
> - `pause` to `onPause`
> - `stop` to `onStop`
> - Removed `onActivitySaveInstanceState`
> - `onActivityDestroy` to `onDestroy`
> - `onActivityLowMemory` to `onLowMemory`
>
> `onLowMemory` no longer takes any parameters.
> `onFirstActivityCreate` no longer takes any parameters.

and also emitting them as window-specific events.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ sha2 = "0.10"
base64 = "0.22"
jni = "0.21"
ndk = "0.9"
tao-macros = "0.1"
tao-macros = "0.1.4"
libc = "0.2"
dom_query = { version = "0.28.0", default-features = false }

[dev-dependencies]
pollster = "1.0.0"
tao = "0.35"
tao = "0.36"
wgpu = "23"
winit = "0.30"
getrandom = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions src/android/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! android_binding {
($domain:ident, $package:ident, $wry:path) => {{
use $wry::{android_setup as _, prelude::*};

android_fn!($domain, $package, Rust, wryCreate, []);
android_fn!($domain, $package, Rust, onFirstActivityCreateWry, []);
android_fn!(
$domain,
$package,
Expand Down Expand Up @@ -274,7 +274,7 @@ fn handle_request(
}

#[allow(non_snake_case)]
pub unsafe fn wryCreate(env: JNIEnv, _: JClass) {
pub unsafe fn onFirstActivityCreateWry(env: JNIEnv, _: JClass) {
let mut main_pipe = MainPipe { env };

let looper = ThreadLooper::for_thread().unwrap();
Expand Down
21 changes: 10 additions & 11 deletions src/android/kotlin/Rust.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ object Rust {
System.loadLibrary("{{library}}")
}

@JvmStatic external fun onActivityCreate(activity: WryActivity)
@JvmStatic external fun onActivityDestroy(activity: WryActivity)
@JvmStatic external fun onActivitySaveInstanceState()
@JvmStatic external fun onActivityLowMemory()
// Tao bindings
@JvmStatic external fun onFirstActivityCreate()
@JvmStatic external fun onCreate(activity: WryActivity)
@JvmStatic external fun onStart(activity: WryActivity)
@JvmStatic external fun onResume(activity: WryActivity)
@JvmStatic external fun onPause(activity: WryActivity)
@JvmStatic external fun onStop(activity: WryActivity)
@JvmStatic external fun onDestroy(activity: WryActivity)
@JvmStatic external fun onWindowFocusChanged(activity: WryActivity, focus: Boolean)
@JvmStatic external fun onLowMemory()
@JvmStatic external fun onNewIntent(intent: Intent)

@JvmStatic external fun create()
@JvmStatic external fun start(activity: WryActivity)
@JvmStatic external fun resume(activity: WryActivity)
@JvmStatic external fun pause(activity: WryActivity)
@JvmStatic external fun stop(activity: WryActivity)

@JvmStatic external fun wryCreate()
@JvmStatic external fun onFirstActivityCreateWry()
@JvmStatic external fun onWebviewDestroy(activity: WryActivity, webviewId: String)

@JvmStatic external fun ipc(webviewId: String, url: String, message: String)
Expand Down
25 changes: 12 additions & 13 deletions src/android/kotlin/WryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.webkit.WebView
import android.view.KeyEvent
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner

private val ACTIVITY_ID_KEY = "__wryActivityId"
private const val ACTIVITY_ID_KEY = "__wryActivityId"

object WryLifecycleObserver : DefaultLifecycleObserver {
// This only runs once: https://developer.android.com/reference/androidx/lifecycle/ProcessLifecycleOwner
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
Rust.create()
Rust.wryCreate()
Rust.onFirstActivityCreate()
Rust.onFirstActivityCreateWry()
}
}

Expand Down Expand Up @@ -125,13 +124,13 @@ abstract class WryActivity : AppCompatActivity() {
activityListener?.invoke(result)
}

Rust.onCreate(this)
ProcessLifecycleOwner.get().lifecycle.addObserver(WryLifecycleObserver)
Rust.onActivityCreate(this)
}

override fun onStart() {
super.onStart()
Rust.start(this)
Rust.onStart(this)
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
Expand All @@ -142,39 +141,38 @@ abstract class WryActivity : AppCompatActivity() {
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(ACTIVITY_ID_KEY, id)
Rust.onActivitySaveInstanceState()
}

override fun onPause() {
super.onPause()
Rust.pause(this)
Rust.onPause(this)
if (::mWebView.isInitialized) {
mWebView.onPause()
}
}

override fun onResume() {
super.onResume()
Rust.resume(this)
Rust.onResume(this)
if (::mWebView.isInitialized) {
mWebView.onResume()
}
}

override fun onStop() {
super.onStop()
Rust.stop(this)
Rust.onStop(this)
}

override fun onDestroy() {
super.onDestroy()
Rust.onActivityDestroy(this)
Rust.onDestroy(this)
Rust.onWebviewDestroy(this, if (::mWebView.isInitialized) { mWebView.id } else { "" })
}

override fun onLowMemory() {
super.onLowMemory()
Rust.onActivityLowMemory()
Rust.onLowMemory()
}

override fun onNewIntent(intent: Intent) {
Expand All @@ -186,6 +184,7 @@ abstract class WryActivity : AppCompatActivity() {
return Class.forName(name)
}

// Called by tao through JNI
fun startActivity(cls: Class<*>): Int {
val intent = Intent(this, cls)
val id = kotlin.random.Random.nextInt()
Expand Down
4 changes: 4 additions & 0 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub fn destroy_webview(activity_id: ActivityId, webview_id: &WebviewId) {
///
/// This function must be run on the thread where the [`JNIEnv`] is registered and the looper is local,
/// hence the requirement for a [`ThreadLooper`].
///
/// When used with tao, this is usually passed in like
/// `tao::android_binding!($domain, $app_name, $activity, android_setup, $main)`
/// to fill in `on_activity_create` which is run at the end of `onCreate` of an activiy
pub unsafe fn android_setup(
package: &str,
mut env: JNIEnv,
Expand Down
Loading