Skip to content

Commit

Permalink
ndk-glue: Finish native activity after returning from main()
Browse files Browse the repository at this point in the history
When an application returns its control flow back from `main()` it is
generally assumed to be done polling on the looper and wishes to
terminate.  Android is made aware of this by calling
`ANativeActivity_finish`, otherwise it'll continue sending input events
and eventually ANR as the app didn't consume them anymore.
  • Loading branch information
MarijnS95 committed Jul 24, 2021
1 parent 49e8ed5 commit 68af0ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ndk-glue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Reexport `android_logger` and `log` from the crate root for `ndk-macro` to use.
- Use new `FdEvents` `bitflags` for looper file descriptor events.
- **Breaking** `NativeActivity` is now dropped (calling `ANativeActivity_finish()`
under the hood) after returning from `fn main()`.

# 0.3.0 (2021-01-30)

Expand Down
6 changes: 5 additions & 1 deletion ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ pub unsafe fn init(
signal_looper_ready.notify_one();
}

main()
main();

NATIVE_ACTIVITY
.take()
.expect("Native activity should not have been taken before main() returns!")
});

// Don't return from this function (`ANativeActivity_onCreate`) until the thread
Expand Down
2 changes: 2 additions & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unreleased

- **Breaking** Model looper file descriptor events integer as `bitflags`.
- **Breaking** `NativeActivity` now calls `ANativeActivity_finish()` on
drop instead of relying on `NativeActivity::finish()` to be called.

# 0.3.0 (2021-01-30)

Expand Down
12 changes: 7 additions & 5 deletions ndk/src/native_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ impl NativeActivity {
/// The relevant NDK docs can be found
/// [here.](https://developer.android.com/ndk/reference/group/native-activity)
impl NativeActivity {
/// Sends a destroy event to the activity and stops it.
pub fn finish(&self) {
unsafe { ffi::ANativeActivity_finish(self.ptr.as_ptr()) }
}

/// Shows the IME (the on-screen keyboard).
///
/// If `force` is true, the `SHOW_FORCED` flag is used; otherwise, the `SHOW_IMPLICIT` flag is
Expand Down Expand Up @@ -176,6 +171,13 @@ impl NativeActivity {
}*/
}

impl Drop for NativeActivity {
/// Sends a destroy event to the activity and stops it.
fn drop(&mut self) {
unsafe { ffi::ANativeActivity_finish(self.ptr.as_ptr()) }
}
}

/*#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum WindowFormat {
Expand Down

0 comments on commit 68af0ab

Please sign in to comment.