3

I'm involved with the FlutterSound project which is shipped as a package containing an api that, for the purposes of this question, doesn't contain a widget.

The api needs to handle events when the application changes its state (AppLifecycleState.pause/resume). (we need to stop/resume audio when the app is paused/resumed).

I can see how to do this in a widget using WidgetsBindingObserver but the api needs this same info without having to rely on a widget.

The SchedulerBinding class has a method handleAppLifecycleStateChanged which seems to provide the required info but its unclear how to implement this outside of a widget.

Brett Sutton
  • 3,900
  • 2
  • 28
  • 53
  • I believe you have to have at least one widget anyway to launch your app. Do you think you can make use of that widget for this purpose? (I am stuck with a similar issue too and was wondering whether there's a direct way to retrieve app lifecycle state; something like: getAppLifecycleState(). Can't seem to find anything like so. If there's no such thing, I'd have to leverage the root widget of my app) – Tanvir May 03 '20 at 09:15
  • I'm building a library that can be used as an API.. I don't want to force users of the library to have to use a widget I impose on them. – Brett Sutton May 04 '20 at 11:01
  • Thanks for clarifying. I came up with a solution for my project; Please check out my answer below – Tanvir May 04 '20 at 23:25

1 Answers1

5

Below is a code sample that can listen to AppLifecycleState change events without directly involving a widget:

import 'package:flutter/material.dart';

class MyLibrary with WidgetsBindingObserver {
  AppLifecycleState _state;

  AppLifecycleState get state => _state;

  MyLibrary() {
    WidgetsBinding.instance.addObserver(this);
  }

  /// make sure the clients of this library invoke the dispose method
  /// so that the observer can be unregistered
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    this._state = state;
  }

  void someFunctionality() {
    // your library feature
  }
}

Now you can instantiate the library in a flutter widget, for instance, from which point it will start listening to any change in AppLifecycleState.

Please note that, the above code doesn't take care of redundant bindings. For example, if the client of your library is meant to initialize the library more than once in different places, the didChangeAppLifecycleState() method will be triggered multiple times per state change (depending on the number of instances of the library that were created). Also I'm unsure whether the solution proposed conforms to flutter best practices. Nevertheless it solves the problem and hope it helps!

Tanvir
  • 542
  • 7
  • 16