I created an Android app that uses NativeActivity
. It starts up and as desired, calls my android_main(app: AndroidApp)
function that I defined in Rust. Here are the docs for android_activity::AndroidApp.
There is another type, in the ndk
crate, called NativeActivity. It has a method I'd like to call, internal_data_path(). But I can't figure out how to go from that AndroidApp
to a NativeActivity
.
I tried writing the code below, but I'm pretty sure that pointer cast there is bad. Because once I try to call internal_data_path()
on that NativeActivity
, the app crashes.
// Set this when app starts up.
static mut ANDROID_APP: Option<AndroidApp> = None;
pub fn native_activity() -> NativeActivity {
unsafe {
let app = ANDROID_APP.as_ref().unwrap();
let ptr = NonNull::new(app.activity_as_ptr() as *mut _).unwrap();
NativeActivity::from_ptr(ptr)
}
}
...
#[no_mangle]
fn android_main(app: AndroidApp) {
unsafe { ANDROID_APP = Some(app.clone()); }
...
Docs for AndroidApp::activity_as_ptr().