Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
19 changes: 19 additions & 0 deletions .changes/android-lifecycle-function-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"tao": minor
---

**Breaking Change:**

Changed the names of the Android JNI lifecycle functions exposed by `android_binding!` as follows:

- `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.
62 changes: 32 additions & 30 deletions src/platform_impl/android/ndk_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ const DATA_URL_ENCODING_SET: &AsciiSet = &CONTROLS
/// 1. android app domain name in reverse snake_case as an ident (for ex: com_example)
/// 2. android package anme (for ex: wryapp)
/// 3. the android activity that has external linking for the following functions and calls them:
/// - `private external fun onActivityCreate(activity: WryActivity)``
/// - `private external fun start(activity: WryActivity)`
/// - `private external fun resume(activity: WryActivity)`
/// - `private external fun pause(activity: WryActivity)`
/// - `private external fun stop(activity: WryActivity)`
/// - `private external fun onActivitySaveInstanceState()`
/// - `private external fun onActivityDestroy(activity: WryActivity)`
/// - `private external fun onActivityLowMemory()`
/// - `private external fun onFirstActivityCreate(activity: WryActivity)`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we currently don't give it that in wry - we can i think because the lifecycle owner is the activity

if we were i'm thinking about moving the ndk-context thing into here but it sucks that it's called after the activity's onCreate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we currently don't give it that in wry - we can i think because the lifecycle owner is the activity

We do, it should be easier to see after tauri-apps/wry#1790

if we were i'm thinking about moving the ndk-context thing into here but it sucks that it's called after the activity's onCreate

I don't mind it but I'd probably keep it just for the timing, I could make a comment on the reasons if you want

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it but I'd probably keep it just for the timing, I could make a comment on the reasons if you want

Talking about timing, i wonder if we even should use the lifecycle thing or if we just just call it in onActivity if there's a way to have a variable across all activities to check if it has been called already. The incorrect timing here really puts me off.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't: https://github.com/tauri-apps/wry/pull/1790/changes#diff-76280dd9be7cec50ab33d778adb7380d01ebfe63a7b5373ee637ef710fe60322R29 - i was talking about the function argument.

Ah, I see, was testing it in tauri and it worked so didn't notice

Talking about timing, i wonder if we even should use the lifecycle thing or if we just just call it in onActivity if there's a way to have a variable across all activities to check if it has been called already. The incorrect timing here really puts me off.

Trying something like tauri-apps/wry@11dab6b but I really don't know what java land smokes that if this works. I would honestly say just leave this alone since it didn't cause any problems so far.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timing is actually correct I think, we need to run the android entry point after we setup many things.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying something like tauri-apps/wry@11dab6b but I really don't know what java land smokes that if this works. I would honestly say just leave this alone since it didn't cause any problems so far.

That works in the multiwindow example i tested everything else at least

The timing is actually correct I think, we need to run the android entry point after we setup many things.

we're talking about https://github.com/tauri-apps/tao/blob/dev/src/platform_impl/android/ndk_glue.rs#L330 and https://github.com/tauri-apps/tao/blob/dev/src/platform_impl/android/ndk_glue.rs#L389 right? to me create looks like it wants to run first.

I almost want to merge these 2 callbacks, we could do the isFirstActivity check in there instead but i think i'm just going crazy - we can keep everything as is and focus on getting it released instead 😂

@Legend-Master Legend-Master Jul 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're talking about https://github.com/tauri-apps/tao/blob/dev/src/platform_impl/android/ndk_glue.rs#L330 and https://github.com/tauri-apps/tao/blob/dev/src/platform_impl/android/ndk_glue.rs#L389 right? to me create looks like it wants to run first.

Yeah, it looks like so but it doesn't and it's probably correct (not semantically, functionally) 😂

I would honestly just leave it to another day, I have documented the timings for now.

/// - `private external fun onCreate(activity: WryActivity)`
/// - `private external fun onStart(activity: WryActivity)`
/// - `private external fun onResume(activity: WryActivity)`
/// - `private external fun onPause(activity: WryActivity)`
/// - `private external fun onStop(activity: WryActivity)`
/// - `private external fun onDestroy(activity: WryActivity)`
/// - `private external fun onWindowFocusChanged(activity: WryActivity, focus: Boolean)`
/// 4. a on_activity_create function that will be ran once after the `onActivityCreate` function above.
/// - `private external fun onLowMemory()`
/// - `private external fun onNewIntent(intent: Intent)`
/// 4. an on_activity_create function that will be ran once after the `onCreate` function above.
/// 5. the main entry point of your android application.
#[rustfmt::skip]
#[macro_export]
Expand All @@ -89,7 +90,7 @@ macro_rules! android_binding {
$domain,
$package,
$activity,
create,
onFirstActivityCreate,
[JObject],
__VOID__,
[$main],
Expand All @@ -98,20 +99,19 @@ macro_rules! android_binding {
$domain,
$package,
$activity,
onActivityCreate,
onCreate,
[JObject],
__VOID__,
[$on_activity_create],
_____tao_store_package_name__,
);
android_fn!($domain, $package, $activity, start, [JObject]);
android_fn!($domain, $package, $activity, stop, [JObject]);
android_fn!($domain, $package, $activity, resume, [JObject]);
android_fn!($domain, $package, $activity, pause, [JObject]);
android_fn!($domain, $package, $activity, onActivitySaveInstanceState, [JObject]);
android_fn!($domain, $package, $activity, onActivityDestroy, [JObject]);
android_fn!($domain, $package, $activity, onActivityLowMemory, [JObject]);
android_fn!($domain, $package, $activity, onWindowFocusChanged, [JObject,i32]);
android_fn!($domain, $package, $activity, onStart, [JObject]);
android_fn!($domain, $package, $activity, onStop, [JObject]);
android_fn!($domain, $package, $activity, onResume, [JObject]);
android_fn!($domain, $package, $activity, onPause, [JObject]);
android_fn!($domain, $package, $activity, onDestroy, [JObject]);
android_fn!($domain, $package, $activity, onWindowFocusChanged, [JObject, i32]);
android_fn!($domain, $package, $activity, onLowMemory, []);
android_fn!($domain, $package, $activity, onNewIntent, [JObject]);
}};
}
Expand Down Expand Up @@ -327,7 +327,8 @@ pub enum WindowEvent {
Destroyed,
}

pub unsafe fn create(_env: JNIEnv, _: JClass, _: JObject, main: fn()) {
#[allow(non_snake_case)]
pub unsafe fn onFirstActivityCreate(_env: JNIEnv, _: JClass, _: JObject, main: fn()) {
let logpipe = {
let mut logpipe: [RawFd; 2] = Default::default();
libc::pipe(logpipe.as_mut_ptr());
Expand Down Expand Up @@ -386,7 +387,7 @@ pub unsafe fn create(_env: JNIEnv, _: JClass, _: JObject, main: fn()) {
}

#[allow(non_snake_case)]
pub unsafe fn onActivityCreate(
pub unsafe fn onCreate(
mut env: JNIEnv,
_jclass: JClass,
activity: JObject,
Expand Down Expand Up @@ -471,7 +472,8 @@ pub unsafe fn onActivityCreate(
handle_intent(env, intent);
}

pub unsafe fn resume(mut env: JNIEnv, _: JClass, activity: JObject) {
#[allow(non_snake_case)]
pub unsafe fn onResume(mut env: JNIEnv, _: JClass, activity: JObject) {
let activity_id = activity_id(&mut env, &activity);
let did_resume = !RESUMED_ACTIVITIES.lock().unwrap().insert(activity_id);
// first Activity onResume() is called even after onCreate()
Expand All @@ -483,7 +485,8 @@ pub unsafe fn resume(mut env: JNIEnv, _: JClass, activity: JObject) {
}
}

pub unsafe fn pause(mut env: JNIEnv, _: JClass, activity: JObject) {
#[allow(non_snake_case)]
pub unsafe fn onPause(mut env: JNIEnv, _: JClass, activity: JObject) {
let activity_id = activity_id(&mut env, &activity);
wake(Event::Pause {
id: WindowId(super::WindowId(activity_id)),
Expand Down Expand Up @@ -701,15 +704,17 @@ pub unsafe fn handle_intent(mut env: JNIEnv, intent: JObject) {
}
}

pub unsafe fn start(mut env: JNIEnv, _: JClass, activity: JObject) {
#[allow(non_snake_case)]
pub unsafe fn onStart(mut env: JNIEnv, _: JClass, activity: JObject) {
let activity_id = activity_id(&mut env, &activity);
wake(Event::WindowEvent {
id: WindowId(super::WindowId(activity_id)),
event: WindowEvent::Started,
});
}

pub unsafe fn stop(mut env: JNIEnv, _: JClass, activity: JObject) {
#[allow(non_snake_case)]
pub unsafe fn onStop(mut env: JNIEnv, _: JClass, activity: JObject) {
let activity_id = activity_id(&mut env, &activity);
wake(Event::WindowEvent {
id: WindowId(super::WindowId(activity_id)),
Expand All @@ -718,7 +723,7 @@ pub unsafe fn stop(mut env: JNIEnv, _: JClass, activity: JObject) {
}

#[allow(non_snake_case)]
pub unsafe fn onActivityDestroy(mut env: JNIEnv, _: JClass, activity: JObject) {
pub unsafe fn onDestroy(mut env: JNIEnv, _: JClass, activity: JObject) {
let activity_id = env
.call_method(&activity, "getId", "()I", &[])
.unwrap()
Expand Down Expand Up @@ -746,10 +751,7 @@ pub unsafe fn onActivityDestroy(mut env: JNIEnv, _: JClass, activity: JObject) {
///////////////////////////////////////////////

#[allow(non_snake_case)]
pub unsafe fn onActivitySaveInstanceState(_: JNIEnv, _: JClass, _: JObject) {}

#[allow(non_snake_case)]
pub unsafe fn onActivityLowMemory(_: JNIEnv, _: JClass, _: JObject) {
pub unsafe fn onLowMemory(_: JNIEnv, _: JClass) {
wake(Event::LowMemory);
}

Expand Down
Loading