Skip to content

Commit 842ac4b

Browse files
committed
Stride.Games/Android: guard ringer receiver register/unregister against pause-before-OnRun
Launching while the device is asleep fires onResume/onPause on the UI thread before Silk's OnRun (separate thread) created the receiver, so OnPause called UnregisterReceiver with a null field and crashed. Create the receiver lazily and track registration under a lock so the two threads stay balanced.
1 parent decb0c6 commit 842ac4b

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

sources/engine/Stride.Games/Starter/StrideActivity.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ protected override void OnDestroy()
5757

5858
protected override void OnRun()
5959
{
60-
// set up a listener to the android ringer mode (Normal/Silent/Vibrate)
61-
ringerModeIntentReceiver = new RingerModeIntentReceiver((AudioManager)GetSystemService(AudioService));
62-
RegisterReceivers();
63-
6460
// Set the android global context
6561
if (PlatformAndroid.Context == null)
6662
PlatformAndroid.Context = this;
@@ -170,8 +166,11 @@ internal void HideEditTextPopup(PopupWindow popupWindow)
170166
});
171167
}
172168

169+
// Created lazily here (called from OnResume), not in OnRun: OnRun is on a separate thread
170+
// that may not have run when a launch-while-asleep pauses us, which would unregister a null.
173171
private void RegisterReceivers()
174172
{
173+
ringerModeIntentReceiver ??= new RingerModeIntentReceiver((AudioManager)GetSystemService(AudioService));
175174
var ringerModeIntentFilter = new IntentFilter(AudioManager.RingerModeChangedAction);
176175
if (OperatingSystem.IsAndroidVersionAtLeast(34))
177176
{

0 commit comments

Comments
 (0)