Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
How to add some Check on Resume (return to your App) in Flutter

How to add some Check on Resume (return to your App) in Flutter

Posted on January 28, 2025 by Toma Velev

To add a Check in Flutter when an App executes Resume  from a background or terminated state, you can use the WidgetsBinding class’s addObserver method to listen for the LifecycleState changes. Here are some steps you can follow:

  1. Create a class that extends WidgetsBindingObserver:
    This is where you’ll put your resume logic. You can add checks and perform actions when the app resumes.
  2. Add observer to WidgetsBinding:
    In your widget’s build method, add the observer.

Here is an example:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show SystemChrome.setPreferredOrientations;
import 'package:flutter/widgets.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        // Resume logic here
        print('App resumed');
        break;
      case AppLifecycleState.paused:
        // Pause logic here
        print('App paused');
        break;
      case AppLifecycleState.detached:
        // Detach logic here
        print('App detached');
        break;
      case AppLifecycleState.inactive:
        // Inactive logic here
        print('App inactive');
        break;
// not present anymore in the api      case AppLifecycleState.unknown

case AppLifecycleState.hidden:

       // Unknown logic here
        print('App hidden');
        break;
    }
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    // Metrics change logic here
  }

  @override
  void didChangeLocales() {
    super.didChangeLocales();
    // Locales change logic here
  }

  @override
  void didChangePlatformBrightness() {
    super.didChangePlatformBrightness();
    // Brightness change logic here
  }

  @override
  void didUpdateWidget(MyHomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    // Update widget logic here
  }

  @override
  void didUpdateWidget(MyHomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    // Update widget logic here
  }

  @override
  void dispose() {
    super.dispose();
  }
}

In the above code, didChangeAppLifecycleState is where you’ll put your resume logic. This method gets called when the app’s lifecycle state changes.

Note: Don’t forget to remove the observer in dispose method when your widget is removed from the tree.

Example use case:

You can add checks and perform actions when the app resumes, such as:

  • Checking if there are any new notifications
  • Fetching data from the server to update your app’s state
  • Showing a welcome message or tutorial when the user resumes the app after a long time
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    case AppLifecycleState.resumed:
      // Fetch data from server to update app's state
      fetchAndUpdateData();
      break;
  }
}

void fetchAndUpdateData() async {
  // Fetch data from server
  final data = await fetchDataFromServer();

  // Update app's state with the fetched data
  setState(() {
    // Update your widget's state here
  });
}

Very obvious example where resume check may be needed is when your app requires some permissions. https://programtom.com/dev/2025/01/22/example-of-how-to-use-the-permission_handler-package-in-flutter/. Both iOS and Android users may pause your app, remove the permissions and return, resulting in a situation – where the app cannot complete the functionality it is programmed to. So, is is always good idea to program defensively.

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)