when i open my app or runs in the background the didChangeAppLifecycleState() is not called and the print statements are not executed.

   import 'package:flutter/widgets.dart';
   import 'package:flutter/material.dart';

   void main(){
    runApp(
    MaterialApp(
      home: Home(),
    )
   );
   }

   class Home extends StatefulWidget {
   @override
    _HomeState createState() => _HomeState();
   }

   class _HomeState extends State<Home> with WidgetsBindingObserver{
   AppLifecycleState state;
   @override
   void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
   }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void didChangeAppLifeCycleState(AppLifecycleState appLifecycleState) {
    state = appLifecycleState;
    print(appLifecycleState);
    print(":::::::");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child:Text("hi")
        ),
      ),
    );
  }
}
}

the print statements in the didChangeAppLifeCycleState() is not executing.


Solution 1: Phuc Tran

There was a typo (lowercase "c" in "Lifecycle"), it should be didChangeAppLifecycleState:

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
   state = state;
   print(state);
   print(":::::::");
}

Update: Don't forget to add observer in initState() and remove observer in dispose()

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

// ...

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

Hope it helps!


Solution 2: user1978019

This isn't the answer to the OP's problem, every time that I have had this problem I have just forgotten to call WidgetsBinding.instance.addObserver(this); in initState().

If didChangeAppLifecycleState isn't called for you, don't forget to add your implementing class as a listener.

See the OP's code in initState() and dispose() and make sure you do the same.


Solution 3: Abdul Saleem

I had to combine codes from the question and the above 2 answers to get the final result. So let me add a simple sample that illustrates how to use AppLifeCycleState properly.

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
   @override
   _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> 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:
        // --
        print('Resumed');
        break;
      case AppLifecycleState.inactive:
        // --
        print('Inactive');
        break;
      case AppLifecycleState.paused:
        // --
        print('Paused');
        break;
      case AppLifecycleState.detached:
        // --
        print('Detached');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }

}