Repository navigation
How to manually manage the lifecycle of SDL on Android targets #266
|
Is there a way to manually manage the SDL lifecycle on an Android target without inheriting from SDLActivity? The currently applied Activity has already inherited AvaloniaActivity, so there is no way to inherit SDLActivity. |
Replies: 3 comments 4 replies
|
Thanks for the question. At the moment, SDL3-CS does not expose a supported way to host the full SDL Android lifecycle inside an existing The practical supported option today is to keep If SDL must render inside the same Avalonia screen, could you share a minimal reproducer and clarify:
With that information, I can investigate an |
|
Below is a minimal two-Activity example that keeps Avalonia as the launcher and lets the official SDL bridge manage SDL's Android lifecycle. Add the packages to the Android head project: <ItemGroup>
<PackageReference Include="SDL3-CS" Version="3.4.14.1" />
<PackageReference Include="SDL3-CS.Android" Version="3.4.14.1" />
</ItemGroup>Keep your existing Avalonia Activity as the launcher. Add a method like this to it (or wrap it in an Android-specific service that your Avalonia view model calls): using Android.Content;
public void OpenSdlScreen()
{
StartActivity(new Intent(this, typeof(SdlGameActivity)));
}Then add a separate using Android.App;
using Android.Content.PM;
using Org.Libsdl.App;
using SDL = SDL3.SDL;
using static Android.Content.PM.ConfigChanges;
namespace YourApp.Android;
[Activity(
Label = "SDL screen",
Exported = false,
LaunchMode = LaunchMode.SingleTop,
ConfigurationChanges =
LayoutDirection | Locale | GrammaticalGender | FontScale |
FontWeightAdjustment | ConfigChanges.Orientation | UiMode |
ScreenLayout | ScreenSize | SmallestScreenSize |
Keyboard | KeyboardHidden | Navigation
)]
public sealed class SdlGameActivity : SDLActivity
{
protected override string[] GetLibraries() => ["SDL3"];
protected override void Main()
{
if (!SDL.Init(SDL.InitFlags.Video))
{
SDL.LogError(
SDL.LogCategory.Application,
$"SDL initialization failed: {SDL.GetError()}");
return;
}
try
{
if (!SDL.CreateWindowAndRenderer(
"SDL from Avalonia",
1280,
720,
0,
out var window,
out var renderer))
{
SDL.LogError(
SDL.LogCategory.Application,
$"Window creation failed: {SDL.GetError()}");
return;
}
try
{
SDL.SetRenderVSync(renderer, 1);
var running = true;
while (running)
{
while (SDL.PollEvent(out var e))
{
if ((SDL.EventType)e.Type == SDL.EventType.Quit)
{
running = false;
}
}
SDL.SetRenderDrawColor(renderer, 24, 32, 48, 255);
SDL.RenderClear(renderer);
SDL.RenderPresent(renderer);
}
}
finally
{
SDL.DestroyRenderer(renderer);
SDL.DestroyWindow(window);
}
}
finally
{
SDL.Quit();
}
}
}Do not set This example uses a separate fullscreen SDL screen. Embedding the SDL surface inside the same Avalonia visual tree would require a different integration layer; a minimal reproducer would help me investigate that path. |
|
@Genouka Thank you for sharing the repository. I checked out the full project at commit I still cannot reproduce the silence:
The published repository does not include the game data/audio bytes that fail on your device, so the most useful next step would be a public minimal repository or a dedicated branch that reproduces the problem automatically on startup. It can be based on your current Android head; it only needs the smallest redistributable failing audio payload (or a script that produces it), the exact commit and run command, and no game data/UI beyond what is required to trigger playback. Please also include:
One line currently hides the most valuable evidence: audio = Mixer.LoadAudioIO(mixer, io, predecode: true, closeio: true);
if (audio == IntPtr.Zero)
throw new InvalidOperationException($"MIX_LoadAudioIO failed: {SDL.GetError()}");I would also set I opened #274 to track the verified Avalonia audio-only example, diagnostics, lifecycle ownership, and Release smoke coverage. Once you share the failing branch/payload, I can run that exact case on both the emulator and the physical ARM64 device. |
@Genouka Thanks, that clarification changes the solution: for audio only, you can keep a single
AvaloniaMainActivity. You do not needSDLActivity,SDLSurface, or the SDL video subsystem.I built and ran a minimal consumer on an Android API 35 x86_64 emulator with Avalonia 12.1.1,
SDL3-CS/SDL3-CS.Android3.4.14.1, andSDL3-CS.Android.Mixer3.2.4.9. It stayed in the Avalonia Activity and successfully played a WAV through SDL_mixer.Add these packages to the Android head project: