Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.12: Android suspend and resume #773

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions content/news/2023-10-21-bevy-0.12/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,43 @@ Since our last release a few months ago we've added a _ton_ of new features, bug

<!-- more -->

## Feature Name
## Suspend and Resume on Android

<div class="release-feature-authors">authors: @author</div>
<div class="release-feature-authors">authors: @mockersf</div>

On Android, applications are no longer crashing on suspend. Instead, they are paused, and no system
mockersf marked this conversation as resolved.
Show resolved Hide resolved
are running until the application is resumed.
mockersf marked this conversation as resolved.
Show resolved Hide resolved

<video controls><source src="suspend-resume.mp4" type="video/mp4"/></video>

Background tasks working in other threads, like playing audio, won't be stopped. When the
application will be suspended, a [`Lifetime`] event `ApplicationLifetime::Suspended` is sent,
corresponding to the [`onStop()`] callback. You should take care to pause tasks that shouldn't
run in the background, and resume them when receiving the `ApplicationLifetime::Resumed` event
(corresponding to the [`onRestart()`] callback).

```rust
fn handle_lifetime_events(
mut lifetime_events: EventReader<ApplicationLifetime>,
music_controller: Query<&AudioSink>,
) {
for event in lifetime_events.read() {
match event {
// Upon receiving the `Suspended` event, the application has 1 frame before it is paused
// As audio happens in an independent thread, it needs to be stopped
ApplicationLifetime::Suspended => music_controller.single().pause(),
// On `Resumed``, audio can continue playing
ApplicationLifetime::Resumed => music_controller.single().play(),
// `Started` is the only other event for now, more to come in the next Bevy version
_ => (),
}
}
}
```

[`Lifetime`]: https://docs.rs/bevy/0.12.0/bevy/window/enum.Lifetime.html
[`onStop()`]: https://developer.android.com/reference/android/app/Activity#onStop()
[`onRestart()`]: https://developer.android.com/reference/android/app/Activity#onRestart()

## <a name="what-s-next"></a>What's Next?

Expand Down
Binary file not shown.
Loading