Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppLifcycleState.didChangeLifecycleState( )function is not called when app comes foreground or in background

Tags:

flutter

dart

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.

like image 209
praveen r Avatar asked Jan 31 '19 14:01

praveen r


3 Answers

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!

like image 149
Phuc Tran Avatar answered Oct 23 '22 11:10

Phuc Tran


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.

like image 30
user1978019 Avatar answered Oct 23 '22 11:10

user1978019


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();
  }

}
like image 9
Abdul Saleem Avatar answered Oct 23 '22 11:10

Abdul Saleem