Skip to content

android: use application context where appropriate - #16321

Open
Packetdancer wants to merge 1 commit into
libsdl-org:mainfrom
Packetdancer:android-context
Open

Packetdancer wants to merge 1 commit into
libsdl-org:mainfrom
Packetdancer:android-context

Conversation

@Packetdancer

Copy link
Copy Markdown
Contributor
  • I confirm that I am the author of this code and release it to the SDL project under the zlib license. This contribution does not contain code from other sources, including code generated by a Large Language Model ("AI").

Description

We keep hitting weird lifecycle edge-cases where some Android variant will kill and garbage-collect the activity before we get a shutdown on controllers or audio.

Since we are only using a context in those situations, rather than any of the activity-specific stuff, use the application context (which will stay valid for the lifetime of the process) rather than the activity context (which is not guaranteed to do so).

It's a fairly small and low-impact change that should globally benefit Android SDL apps.

Existing Issue(s)

I don't think we have an actual issue in the issue tracker for this, but slouken and I have both been on an email chain about one. :)

We keep hitting weird lifecycle edge-cases where some Android variant will kill and garbage-collect the activity before we get a shutdown on controllers or audio.

Since we are only using a context in those situations, rather than any of the activity-specific stuff, use the application context (which will stay valid for the lifetime of the process) rather than the activity context (which is not guaranteed to do so).
Comment on lines +85 to +86
}
else {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
else {
} else {

return mContext;
}

static public Context getStaticContext() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static public Context getStaticContext() {
static public Context getApplicationContext() {

mApplicationContext = context.getApplicationContext();
}
else {
mApplicationContext = null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to clear the application context here?

@slouken

slouken commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Extending this a little more, are there cases where we want the activity context instead of the application context, or do we always want the application context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants