Skip to main content

OnCreateState

Struct OnCreateState 

Source
pub struct OnCreateState<'a> { /* private fields */ }
Expand description

The state passed to the optional android_on_create entry point if available.

This gives access to the Java VM, the Java Activity and any saved state from a previous instance of the Activity that was saved via the onSaveInstanceState callback.

Each time android_on_create is called it will receive a new Activity reference.

See the top-level android-activity documentation for more details on android_on_create.

Implementations§

Source§

impl<'a> OnCreateState<'a>

Source

pub fn vm_as_ptr(&self) -> *mut c_void

Returns a pointer to the Java Virtual Machine, for making JNI calls

If you use the jni crate, you can wrap this pointer as a JavaVM via:

let vm = unsafe { JavaVM::from_raw(on_create_state.vm_as_ptr().cast()) };
Source

pub fn activity_as_ptr(&self) -> *mut c_void

Returns an (unowned) JNI global object reference for this Activity as a pointer

If you use the jni crate, you can cast this as a JObject reference via:

let vm = unsafe { JavaVM::from_raw(on_create_state.vm_as_ptr().cast()) };
let _res = vm.attach_current_thread(|env| -> jni::errors::Result<()> {
    let activity = on_create_state.activity_as_ptr() as jni::sys::jobject;
    // SAFETY: The reference / pointer is valid at least until we return from `android_on_create`
    let activity = unsafe { env.as_cast_raw::<JObject>(&activity)? };
    // Do something with `activity` here
    Ok(())
});
§JNI Safety

It is not specified whether this will be a global or local reference and in any case you must treat is as a reference that you do not own and must not attempt to delete it.

  • Don’t wrap the reference as a Global which would try to delete the reference when dropped.
  • Don’t wrap the reference in an Auto which would treat the reference like a local reference and try to delete it when dropped.
Source

pub fn saved_state(&self) -> &[u8]

Returns the saved state of the Activity as a byte slice, which may be empty if there is no saved state.

Auto Trait Implementations§

§

impl<'a> Freeze for OnCreateState<'a>

§

impl<'a> RefUnwindSafe for OnCreateState<'a>

§

impl<'a> !Send for OnCreateState<'a>

§

impl<'a> !Sync for OnCreateState<'a>

§

impl<'a> Unpin for OnCreateState<'a>

§

impl<'a> UnsafeUnpin for OnCreateState<'a>

§

impl<'a> UnwindSafe for OnCreateState<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.