rust-mobile / android-activity

Glue for building Rust applications on Android with NativeActivity or GameActivity
238 stars 46 forks source link

OpenXR failing when moving from ndk-glue to android-activity due to activity ready state #120

Closed sinoth closed 11 months ago

sinoth commented 11 months ago

When running an OpenXR sample using ndk-glue and running on a Meta Quest, the xrSession gets created successfully: OpenXR : xrCreateSession: Activity is already in the ready state.

However when switching to android-activity, the session creation fails with the following message: OpenXR : xrCreateSession: Activity is not yet in the ready state.

The changes made to the sample were as instructed in the readme, going from this:

#[cfg_attr(target_os = "android", ndk_glue::main)]
pub fn main() {

To this:

#[no_mangle]
fn android_main(_: AndroidApp) {

This is using the following feature flag: android-activity = { version="0.4", features = [ "native-activity" ] }

Any idea why the activity seems to not transitioning to the 'ready' state?

rib commented 11 months ago

One thing is that your app will need to call android_app::poll_events() in a main loop to make sure it pulls events regularly from the Java main thread otherwise the Java thread will likely just hang waiting for your native app to acknowledge certain state changes.

You can see a complete openxr android example here: https://github.com/rust-mobile/rust-android-examples/blob/main/na-openxr-wgpu/src/lib.rs and the android_main https://github.com/rust-mobile/rust-android-examples/blob/3ad2b59852159fa98a33e1fd2575f0e89540ac6a/na-openxr-wgpu/src/lib.rs#L1134 illustrates how you can do this.

This minimal mainloop example might also be a useful reference too: https://github.com/rust-mobile/rust-android-examples/blob/main/na-mainloop/src/lib.rs

Hope that helps

sinoth commented 11 months ago

Thanks for the clarification!