Logo

dev-resources.site

for different kinds of informations.

Flutter App life cycle

Published at
9/1/2021
Categories
flutter
dart
lifecycle
Author
prakashselvaraj
Categories
3 categories in total
flutter
open
dart
open
lifecycle
open
Author
15 person written this
prakashselvaraj
open
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 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 {
Enter fullscreen mode Exit fullscreen mode

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;
    }
  }
Enter fullscreen mode Exit fullscreen mode

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);
  }
Enter fullscreen mode Exit fullscreen mode

Ofcourse dont forget to remove observer on dispose

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

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

For Full sample Github

Happy Fluttering 😇😇

lifecycle Article's
30 articles in total
Favicon
Efficiently Deleting Millions of Objects in Amazon S3 Using Lifecycle Policy
Favicon
How to reuse variables in your Lambda across invocations and know when it's about to terminate
Favicon
React js Life cycle
Favicon
The Data Science Lifecycle: From Raw Data to Real-World Results
Favicon
Effortlessly Link Streamlit to Google Sheets for Real-time Analysis
Favicon
DevOps Full Form
Favicon
The Life Cycle of Technology
Favicon
Bridge Between Lifecycle Methods & Hooks
Favicon
Angular Life Cycle Hooks
Favicon
The Lifecycle of React Components
Favicon
The ContentAfterCheck Hook in Angular
Favicon
Angular OnChanges: Building Robust Applications
Favicon
The Life Cycle of Developing a Website 🚴‍♀️
Favicon
Activity and Fragment Lifecycle - Android
Favicon
Simplify Your Data Lifecycle & Optimize Storage Costs With Amazon S3 Lifecycle
Favicon
Vue 3 lifecycle hooks with real-time example
Favicon
Jetpack Compose Lifecycle
Favicon
Intermediate Exploration of some React library concepts
Favicon
End-to-end machine learning lifecycle
Favicon
React Lifecycle Methods in Functional Terms
Favicon
React Hooks - Understanding the useEffect Hook
Favicon
Application probes
Favicon
Flutter App life cycle
Favicon
React Hooks: An Introduction
Favicon
The React Lifecycle Methods: An Introduction
Favicon
React.useEffect()
Favicon
React Component Lifecycle Methods from v16.3 with example
Favicon
Passado e futuro?
Favicon
Active Record Callbacks Introduction
Favicon
React and its Lifecycle Methods Explained

Featured ones: