Skip to content

A way to bind to onActivityResult #303

Closed

Description

@clay53

Currently trying to port one of my programs (winit/wgpu) to Android but I need file selection. Currently, I have the intent being called successfully:

let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }.unwrap();
let env = vm.attach_current_thread().unwrap();

let intent_class = env.find_class("android/content/Intent").unwrap();
let action_open_document = env.get_static_field(intent_class, "ACTION_OPEN_DOCUMENT", "Ljava/lang/String;").unwrap();
let category_openable = env.get_static_field(intent_class, "CATEGORY_OPENABLE", "Ljava/lang/String;").unwrap();
let intent = env.new_object(intent_class, "(Ljava/lang/String;)V", &[action_open_document]).unwrap();
env.call_method(intent, "addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", &[category_openable]).unwrap();
env.call_method(intent, "setType", "(Ljava/lang/String;)Landroid/content/Intent;", &[env.new_string("*/*").unwrap().into()]).unwrap();

let start_activity_result = env.call_method(ctx.context().cast(), "startActivityForResult", "(Landroid/content/Intent;I)V", &[intent.into(), 0.into()]);

info!("{:#?}", start_activity_result);

However, now I've come into the problem where in order to access onActivityResult (https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent), I believe I would have to do it in Java since it requires a method override and not like a native method. I don't know how this could be integrated in this set of tools (maybe have an optional project built into the build process that has native methods that one can override from JNI though idk if that will still get results while the NativeActivity is running). In the meantime, if anyone has any tips on how I can get this working, I'd greatly appreciate it. If I come across something, I'll put it here. I think documentation/examples for stuff like this would also be useful.

Activity

  1. clay53 commented on Jun 26, 2022

    @clay53
    Author

    What I've done to workaround this for now (for file management only) is to use the app-specific storage API (https://developer.android.com/training/data-storage/app-specific) instead as it does not rely on onActivityResult.

  2. locked and limited conversation to collaborators on Dec 22, 2022
  3. converted this issue into a discussion #379 on Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions