如何使用Flutter处理应用程序生命周期(在Android和iOS上)?
颤振应用程序中是否有如何使用Flutter处理应用程序生命周期(在Android和iOS上)?,android,ios,dart,flutter,Android,Ios,Dart,Flutter,颤振应用程序中是否有活动生命周期方法 比如: 或: 如何处理应用程序生命周期?当系统将应用程序放在后台或将应用程序返回前台时,会调用一种方法 小部件示例: class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver { @override void initState() { super.initState(); Widgets
活动
生命周期方法
比如:
或:
如何处理应用程序生命周期?当系统将应用程序放在后台或将应用程序返回前台时,会调用一种方法 小部件示例:
class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() { _notification = state; });
}
@override
Widget build(BuildContext context) {
return new Text('Last notification: $_notification');
}
}
class\u applifecycleActorState扩展状态以了解应用程序可能处于的状态,例如:
不活跃
停顿
恢复
暂停
这些常数的用法是常数的值,例如:
const AppLifecycleState(state)
要在应用程序转到前台或弹出的路径时收到通知,您可以继承LifecycleState
类并重写onResume()
和onPause()
方法<代码>生命周期地产
类别:
/// Inherit this State to be notified of lifecycle events, including popping and pushing routes.
///
/// Use `pushNamed()` or `push()` method to track lifecycle events when navigating to another route.
abstract class LifecycleState <T extends StatefulWidget> extends State<T>
with WidgetsBindingObserver {
ResumeResult resumeResult = new ResumeResult();
bool _isPaused = false;
AppLifecycleState lastAppState = AppLifecycleState.resumed;
void onResume() {}
void onPause() {}
/// Use instead of Navigator.push(), it fires onResume() after route popped
Future<T> push<T extends Object>(BuildContext context, Route<T> route, [String source]) {
_isPaused = true;
onPause();
return Navigator.of(context).push(route).then((value) {
_isPaused = false;
resumeResult.data = value;
resumeResult.source = source;
onResume();
return value;
});
}
/// Use instead of Navigator.pushNamed(), it fires onResume() after route popped
Future<T> pushNamed<T extends Object>(BuildContext context, String routeName, {Object arguments}) {
_isPaused = true;
onPause();
return Navigator.of(context).pushNamed<T>(routeName, arguments: arguments).then((value) {
_isPaused = false;
resumeResult.data = value;
resumeResult.source = routeName;
onResume();
return value;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
if (!_isPaused) {
onPause();
}
} else if (state == AppLifecycleState.resumed &&
lastAppState == AppLifecycleState.paused) {
if (!_isPaused) {
onResume();
}
}
lastAppState = state;
}
}
class ResumeResult {
dynamic data;
String source;
}
///继承此状态以通知生命周期事件,包括弹出和推送路由。
///
///导航到其他路由时,使用'pushNamed()'或'push()'方法跟踪生命周期事件。
抽象类LifecycleState扩展了状态
使用WidgetsBindingObserver{
ResumeResult Result=新ResumeResult();
bool_isPaused=false;
AppLifecycleState lastAppState=AppLifecycleState.resumed;
void onResume(){}
void onPause(){}
///使用代替Navigator.push(),它在弹出路由后激发onResume()
未来推送(BuildContext上下文,路由,[字符串源]){
_isPaused=真;
onPause();
返回Navigator.of(context).push(route).then((value){
_isPaused=false;
resumeResult.data=值;
resumesult.source=源;
onResume();
返回值;
});
}
///使用而不是Navigator.pushName(),它在弹出路由后激发onResume()
Future pushNamed(BuildContext上下文、字符串routeName、{Object arguments}){
_isPaused=真;
onPause();
返回Navigator.of(context).pushNamed(routeName,arguments:arguments)。然后((value){
_isPaused=false;
resumeResult.data=值;
resumeResult.source=routeName;
onResume();
返回值;
});
}
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
if(state==AppLifecycleState.paused){
如果(!\u是有理由的){
onPause();
}
}如果(state==AppLifecycleState.resume&&
lastAppState==AppLifecycleState.paused){
如果(!\u是有理由的){
onResume();
}
}
lastAppState=状态;
}
}
班级成绩{
动态数据;
字符串源;
}
另外,请确保使用
push()
或pushNamed()
方法开始推送新路线。运行以下代码,按home按钮,然后重新打开应用程序,查看其工作情况。有4个:
已恢复:应用程序可见并响应用户输入
非活动:应用程序处于非活动状态,未接收用户输入
暂停:应用程序当前对用户不可见,不响应用户输入,并且在后台运行
分离:应用程序仍然托管在颤振引擎上,但与任何主机视图分离
空安全代码:
类MyPage扩展StatefulWidget{
@凌驾
_MyPageState createState()=>\u MyPageState();
}
类_MyPageState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance!.addObserver(此);
}
@凌驾
无效处置(){
WidgetsBinding.instance!.removeObserver(此);
super.dispose();
}
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
super.didChangeAppLifecycleState(州);
打印(“当前状态=$state”);
}
@凌驾
小部件构建(BuildContext上下文)=>Scaffold();
}
如何完成最后一项活动?喜欢飞溅login@DeepakKanyan我认为这是另一个问题,与此问题无关,您可以问新问题,然后向我推荐,如果可以,我会提供帮助。@DeepakKanyan使用Navigator.pushReplacement()而不是Navigator.push(),那么当前活动将从堆栈中删除此答案与此问题无关
class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() { _notification = state; });
}
@override
Widget build(BuildContext context) {
return new Text('Last notification: $_notification');
}
}
/// Inherit this State to be notified of lifecycle events, including popping and pushing routes.
///
/// Use `pushNamed()` or `push()` method to track lifecycle events when navigating to another route.
abstract class LifecycleState <T extends StatefulWidget> extends State<T>
with WidgetsBindingObserver {
ResumeResult resumeResult = new ResumeResult();
bool _isPaused = false;
AppLifecycleState lastAppState = AppLifecycleState.resumed;
void onResume() {}
void onPause() {}
/// Use instead of Navigator.push(), it fires onResume() after route popped
Future<T> push<T extends Object>(BuildContext context, Route<T> route, [String source]) {
_isPaused = true;
onPause();
return Navigator.of(context).push(route).then((value) {
_isPaused = false;
resumeResult.data = value;
resumeResult.source = source;
onResume();
return value;
});
}
/// Use instead of Navigator.pushNamed(), it fires onResume() after route popped
Future<T> pushNamed<T extends Object>(BuildContext context, String routeName, {Object arguments}) {
_isPaused = true;
onPause();
return Navigator.of(context).pushNamed<T>(routeName, arguments: arguments).then((value) {
_isPaused = false;
resumeResult.data = value;
resumeResult.source = routeName;
onResume();
return value;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
if (!_isPaused) {
onPause();
}
} else if (state == AppLifecycleState.resumed &&
lastAppState == AppLifecycleState.paused) {
if (!_isPaused) {
onResume();
}
}
lastAppState = state;
}
}
class ResumeResult {
dynamic data;
String source;
}