Flutter App life cycle

Basically when we are writing a code for any Native applications, We will look for a life cycle events to handle some specific scenarios. Its like handling Thanos gauntlet snap to blip ??

Flutter comes with life cycle events to handle app life cycle f…


This content originally appeared on DEV Community and was authored by Prakash S

Basically when we are writing a code for any Native applications, We will look for a life cycle events to handle some specific scenarios. Its like handling Thanos gauntlet snap to blip ??

Flutter comes with life cycle events to handle app life cycle for android & ios.

Let's see the code.

To consume the life cycle event, we need to have Stateful widget with WidgetsBindingObserver mixin.

class _HomePageState 
extends State<HomePage> 
with WidgetsBindingObserver {

The mixin WidgetsBindingObserver provides an override didChangeAppLifecycleState where we will notified for certain app life cycle state changes.

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print('app resumed');
        break;
      case AppLifecycleState.paused:
        print('app paused');
        break;
      case AppLifecycleState.inactive:
        print('app inactive');
        break;
      case AppLifecycleState.detached:
      default:
        print('app detached');
        break;
    }
  }

Finally to get work all these stuff, we need to inform or observe the app life cycle changes using

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

Ofcourse dont forget to remove observer on dispose

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

That's it, we can now handle the app as per the app lifecycle.

Happy Fluttering ??


This content originally appeared on DEV Community and was authored by Prakash S


Print Share Comment Cite Upload Translate Updates
APA

Prakash S | Sciencx (2021-09-01T03:46:22+00:00) Flutter App life cycle. Retrieved from https://www.scien.cx/2021/09/01/flutter-app-life-cycle/

MLA
" » Flutter App life cycle." Prakash S | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/flutter-app-life-cycle/
HARVARD
Prakash S | Sciencx Wednesday September 1, 2021 » Flutter App life cycle., viewed ,<https://www.scien.cx/2021/09/01/flutter-app-life-cycle/>
VANCOUVER
Prakash S | Sciencx - » Flutter App life cycle. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/flutter-app-life-cycle/
CHICAGO
" » Flutter App life cycle." Prakash S | Sciencx - Accessed . https://www.scien.cx/2021/09/01/flutter-app-life-cycle/
IEEE
" » Flutter App life cycle." Prakash S | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/flutter-app-life-cycle/. [Accessed: ]
rf:citation
» Flutter App life cycle | Prakash S | Sciencx | https://www.scien.cx/2021/09/01/flutter-app-life-cycle/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.