如何检测Flutter应用何时从后台返回?

时间:2019-11-26 17:44:32

标签: flutter

我想检测Flutter应用何时从后台返回。

在其他用于crossApp开发的SDK中,当应用更改此状态时,通常会有一个侦听器。 颤动中有类似的东西吗?

1 个答案:

答案 0 :(得分:1)

class _AppState extends State<App> with WidgetsBindingObserver {
  var _navigatorKey = GlobalKey<NavigatorState>();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    if (state == AppLifecycleState.paused) {
      _navigatorKey.currentState
          .popUntil((route) => true);
      _navigatorKey.currentState.pushNamed(RouteName.login);

    }
  }
}