AppLifecycleListener should dispatch creation and disposal events. (#137840)
This commit is contained in:
committed by
GitHub
parent
8375dfdeaa
commit
2369897f09
@@ -73,6 +73,15 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable {
|
||||
this.onStateChange,
|
||||
}) : binding = binding ?? WidgetsBinding.instance,
|
||||
_lifecycleState = (binding ?? WidgetsBinding.instance).lifecycleState {
|
||||
// TODO(polina-c): stop duplicating code across disposables
|
||||
// https://github.com/flutter/flutter/issues/137435
|
||||
if (kFlutterMemoryAllocationsEnabled) {
|
||||
MemoryAllocations.instance.dispatchObjectCreated(
|
||||
library: 'package:flutter/widgets.dart',
|
||||
className: '$AppLifecycleListener',
|
||||
object: this,
|
||||
);
|
||||
}
|
||||
this.binding.addObserver(this);
|
||||
}
|
||||
|
||||
@@ -174,6 +183,11 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable {
|
||||
@mustCallSuper
|
||||
void dispose() {
|
||||
assert(_debugAssertNotDisposed());
|
||||
// TODO(polina-c): stop duplicating code across disposables
|
||||
// https://github.com/flutter/flutter/issues/137435
|
||||
if (kFlutterMemoryAllocationsEnabled) {
|
||||
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
|
||||
}
|
||||
binding.removeObserver(this);
|
||||
assert(() {
|
||||
_debugDisposed = true;
|
||||
|
||||
@@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
|
||||
|
||||
void main() {
|
||||
late AppLifecycleListener listener;
|
||||
AppLifecycleListener? listener;
|
||||
|
||||
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
|
||||
final ByteData? message = const StringCodec().encodeMessage(state.toString());
|
||||
@@ -40,7 +40,8 @@ void main() {
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
listener.dispose();
|
||||
listener?.dispose();
|
||||
listener = null;
|
||||
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
|
||||
binding.resetLifecycleState();
|
||||
binding.platformDispatcher.resetInitialLifecycleState();
|
||||
@@ -163,6 +164,16 @@ void main() {
|
||||
await sendAppExitRequest();
|
||||
expect(exitRequested, isTrue);
|
||||
});
|
||||
|
||||
test('AppLifecycleListener dispatches memory events', () async {
|
||||
await expectLater(
|
||||
await memoryEvents(
|
||||
() => AppLifecycleListener(binding: WidgetsBinding.instance).dispose(),
|
||||
AppLifecycleListener,
|
||||
),
|
||||
areCreateAndDispose,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
class TestAppLifecycleListener extends AppLifecycleListener {
|
||||
|
||||
Reference in New Issue
Block a user