mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-28 23:25:52 +01:00
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.
33 lines
1.2 KiB
Dart
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();
|
|
}
|
|
}
|