Zeffry Reynando
Zeffry Reynando

Reputation: 3899

Flutter : Check AppLifecycle event Suspending

In Flutter docs about AppLifecycle Event, it's have 4 Event .

In 4 event above, i can print AppLifecycle event inactive, paused, resumed, but i can't handle suspending event. Because i want check if the user kill/destroy the App from task manager. if the user kill/destroy the app , i want show Security Code (like PinCode etc). How can i handle suspending event ?

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  AppLifecycleState _appLifecycleState;
  FunctionHelper functionHelper = FunctionHelper();
  PageController _pageController;
  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    _pageController = PageController(initialPage: 0);
    super.initState();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
    setState(() {
      _appLifecycleState = appLifecycleState;
    });
    print(appLifecycleState);
  }

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

  @override
  Widget build(BuildContext context) {
    if (_appLifecycleState == null) {
      return Center(
        child: Text('This widget has not observed any lifecycle changes.'),
      );
    } else {
      return Center(
        child: Text(
            'The most recent lifecycle state this widget observed was: $_appLifecycleState'),
      );
    }
}

Upvotes: 1

Views: 2227

Answers (1)

Ayush Bherwani
Ayush Bherwani

Reputation: 2729

Flutter does not have any API to use onDestroy method of Activity flutter/flutter#21982. During the suspending event the application is suspended momentarily which is equivalent to onStop in Android. When user kills or destroys the App, the onDestroy is triggered in Android therefore it cannot be handled using suspending event.

Upvotes: 2

Related Questions