Skip to content

Commit

Permalink
[Android] 与纯Flutter应用生命周期行为保持一致:应用切换到后台时,暂停帧调度,解决动画残影的问题 (#1724)
Browse files Browse the repository at this point in the history
相关问题:#1715, #1686

[注意] 应用「务必」确保前后台通知事件(onBackground/onForeground)的准确性,必要时通过dispatchBackForegroundEvent接管,否则可能出现页面假死现象;
  • Loading branch information
0xZOne committed Oct 13, 2022
1 parent cdc64b4 commit 833ee08
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
1. [Android]fix popUntil not working (#1718)
2. Add demo for afterimage test
3. [Android] 与纯Flutter应用生命周期行为保持一致:应用切换到后台时,暂停帧调度,解决动画残影的问题。[注意] 应用「务必」确保前后台通知事件(onBackground/onForeground)的准确性(必要时通过dispatchBackForegroundEvent接口进行接管),否则可能出现页面假死现象;

## 3.1.2
1. [Android] 提前暴露混淆导致的异常,并给出明确指引
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ public void onForeground() {
CommonParams params = new CommonParams();
channel.onForeground(params, reply -> {
});

// The scheduling frames are resumed when [onForeground] is called.
FlutterBoost.instance().changeFlutterAppLifecycle(FlutterBoost.FLUTTER_APP_STATE_RESUMED);
} else {
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
}
Expand All @@ -263,6 +266,9 @@ public void onBackground() {
CommonParams params = new CommonParams();
channel.onBackground(params, reply -> {
});

// The scheduling frames are paused when [onBackground] is called.
FlutterBoost.instance().changeFlutterAppLifecycle(FlutterBoost.FLUTTER_APP_STATE_PAUSED);
} else {
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
}
Expand Down
41 changes: 39 additions & 2 deletions example/lib/case/rotation_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';

class RotationTranDemo extends StatefulWidget {
RotationTranDemo({Key? key}) : super(key: key);
Expand All @@ -12,11 +13,12 @@ class RotationTranDemo extends StatefulWidget {
}

class RotationTranDemoState extends State<RotationTranDemo>
with TickerProviderStateMixin {
with TickerProviderStateMixin, PageVisibilityObserver {
late AnimationController _controller;
late Animation<double> _animation;

initState() {
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 3000),
Expand All @@ -34,14 +36,18 @@ class RotationTranDemoState extends State<RotationTranDemo>
_controller.addListener(() {
setState(() {});
});
print("RotationTranDemo - initState");
}

@override
void dispose() {
_controller.dispose();
PageVisibilityBinding.instance.removeObserver(this);
super.dispose();
print("RotationTranDemo - dispose");
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
Expand All @@ -62,4 +68,35 @@ class RotationTranDemoState extends State<RotationTranDemo>
),
);
}

@override
void onBackground() {
super.onBackground();
print("RotationTranDemo - onBackground");
}

@override
void onForeground() {
super.onForeground();
print("RotationTranDemo - onForeground");
}

@override
void onPageHide() {
super.onPageHide();
print("RotationTranDemo - onPageHide");
}

@override
void onPageShow() {
super.onPageShow();
print("RotationTranDemo - onPageShow");
}

@override
void didChangeDependencies() {
PageVisibilityBinding.instance.addObserver(this, ModalRoute.of(context)!);
super.didChangeDependencies();
print("RotationTranDemo - didChangeDependencies");
}
}

0 comments on commit 833ee08

Please sign in to comment.