Files
SpotiFLAC-Mobile/lib/services/activity_attachment.dart
T
zarzet d7798898e1 fix(startup): wait for the Activity before backend startup calls
A media service, such as media resumption after boot, can start the
engine before MainActivity registers the backend and platform channels.
The awaited orientation request was then never answered, leaving the
splash screen up forever. Later backend calls failed instead, so
extensions stayed uninitialized until the Extensions page retried them.

Wait for an Activity lifecycle state on Android before startup work
that reaches those channels.
2026-10-20 02:45:00 +07:00

33 lines
1.2 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
/// Completes once an Activity is attached to the Flutter engine.
///
/// A media service (for example media resumption after boot) can start the
/// engine without an Activity. The backend channel is registered only when
/// MainActivity attaches, so startup work that calls it must wait. The engine
/// reports no lifecycle state until an Activity attaches.
Future<void> waitForActivityAttachment(WidgetsBinding binding) {
bool attached(AppLifecycleState? state) =>
state == AppLifecycleState.resumed || state == AppLifecycleState.inactive;
if (attached(binding.lifecycleState)) return Future<void>.value();
final observer = _AttachmentObserver(attached);
binding.addObserver(observer);
return observer.done.future.whenComplete(
() => binding.removeObserver(observer),
);
}
class _AttachmentObserver with WidgetsBindingObserver {
_AttachmentObserver(this._attached);
final bool Function(AppLifecycleState? state) _attached;
final done = Completer<void>();
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (_attached(state) && !done.isCompleted) done.complete();
}
}