Skip to content

Commit

Permalink
#1153 Boost接管handleAppLifecycleStateChanged,让Flutter生命周期与应用前后台对齐
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdPaleLight committed Jun 7, 2021
1 parent aa02956 commit 173c910
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter_boost_example/tab/simple_widget.dart';
void main() {
PageVisibilityBinding.instance
.addGlobalObserver(AppGlobalPageVisibilityObserver());
CustomFlutterBinding();
runApp(MyApp());
}

Expand Down Expand Up @@ -57,6 +58,10 @@ class AppGlobalPageVisibilityObserver extends GlobalPageVisibilityObserver {
}
}

class CustomFlutterBinding extends WidgetsFlutterBinding with BoostFlutterBinding {

}

class CustomInterceptor1 extends BoostInterceptor {
@override
void onPush(BoostInterceptorOption option, PushInterceptorHandler handler) {
Expand Down
37 changes: 37 additions & 0 deletions lib/boost_flutter_binding.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import 'package:flutter/widgets.dart';
import 'package:flutter/scheduler.dart';
import 'logger.dart';

mixin BoostFlutterBinding on WidgetsFlutterBinding {

bool _appLifecycleStateLocked = true;

@override
void initInstances() {
super.initInstances();
_instance = this;
changeAppLifecycleState(AppLifecycleState.resumed);
}

static BoostFlutterBinding get instance => _instance;
static BoostFlutterBinding _instance;

@override
void handleAppLifecycleStateChanged(AppLifecycleState state) {
if (_appLifecycleStateLocked) {
return;
}
Logger.log('boost_flutter_binding: handleAppLifecycleStateChanged ${state.toString()}');
super.handleAppLifecycleStateChanged(state);
}

void changeAppLifecycleState(AppLifecycleState state) {
if (SchedulerBinding.instance.lifecycleState == state) {
return;
}
_appLifecycleStateLocked = false;
handleAppLifecycleStateChanged(state);
_appLifecycleStateLocked = true;
}
}
4 changes: 4 additions & 0 deletions lib/boost_lifecycle_binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart';
import 'boost_container.dart';
import 'logger.dart';
import 'page_visibility.dart';
import 'boost_flutter_binding.dart';

class BoostLifecycleBinding {
BoostLifecycleBinding._();
Expand Down Expand Up @@ -79,11 +80,14 @@ class BoostLifecycleBinding {
Logger.log('boost_lifecycle: BoostLifecycleBinding.appDidEnterForeground');
PageVisibilityBinding.instance
.dispatchPageForgroundEvent(container.topPage.route);

BoostFlutterBinding.instance.changeAppLifecycleState(AppLifecycleState.resumed);
}

void appDidEnterBackground(BoostContainer container) {
Logger.log('boost_lifecycle: BoostLifecycleBinding.appDidEnterBackground');
PageVisibilityBinding.instance
.dispatchPageBackgroundEvent(container.topPage.route);
BoostFlutterBinding.instance.changeAppLifecycleState(AppLifecycleState.paused);
}
}
1 change: 1 addition & 0 deletions lib/flutter_boost.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export 'flutter_boost_app.dart';
export 'logger.dart';
export 'page_visibility.dart';
export 'boost_channel.dart';
export 'boost_flutter_binding.dart';

0 comments on commit 173c910

Please sign in to comment.