Classes and mixins for observing app lifecycle state changes and invoking callbacks when the app state changes.
LifecycleObserver
Base AppLifecycleState callback class. This class observes app lifecycle state changes and invokes the appropriate callbacks when the app state changes.
Properties
Callback invoked when the app enters the resumed state.
Callback invoked when the app enters the paused state.
Callback invoked when the app enters the detached state.
Callback invoked when the app enters the inactive state.
Callback invoked when the app enters the hidden state.
Constructor
LifecycleObserver({
VoidCallback? onResumeCallback,
VoidCallback? onPauseCallback,
VoidCallback? onDetachedCallback,
VoidCallback? onInactiveCallback,
VoidCallback? onHiddenCallback
})
Creates a LifecycleObserver with optional callbacks for each lifecycle state. All callbacks are optional and can be null if not needed.
LifecycleObserverMixin
A mixin that provides lifecycle observation capabilities to any class. This mixin allows classes to observe app lifecycle state changes by automatically setting up a LifecycleObserver and registering it with the widget binding.
Properties
The lifecycle observer instance.
Methods
initLifecycleObserver
void initLifecycleObserver()
Initializes the lifecycle observer with callbacks. This method should be called to set up lifecycle observation. It creates a LifecycleObserver with the mixin’s callback methods and registers it with WidgetsBinding.
onPaused
Called when the app enters the paused state. Override this method to handle app pause events.
onResumed
Called when the app enters the resumed state. Override this method to handle app resume events.
onInactive
Called when the app enters the inactive state. Override this method to handle app inactive events.
onDetached
Called when the app enters the detached state. Override this method to handle app detached events.
onHidden
Called when the app enters the hidden state. Override this method to handle app hidden events.
disposeLifecycleObserver
void disposeLifecycleObserver()
Disposes the lifecycle observer and removes it from the widget binding. This method should be called when lifecycle observation is no longer needed.
LifecycleObserverStateMixin
A mixin that provides lifecycle observation capabilities to State classes. This mixin automatically initializes lifecycle observation in initState and disposes it in dispose, making it convenient for use in StatefulWidgets.
Properties
The lifecycle observer instance.
Methods
onPaused
Called when the app enters the paused state. Override this method to handle app pause events.
onResumed
Called when the app enters the resumed state. Override this method to handle app resume events.
onInactive
Called when the app enters the inactive state. Override this method to handle app inactive events.
onDetached
Called when the app enters the detached state. Override this method to handle app detached events.
onHidden
Called when the app enters the hidden state. Override this method to handle app hidden events.
LifecycleObserverChangeNotifierMixin
A mixin that provides lifecycle observation capabilities to ChangeNotifier classes. The lifecycle observer is automatically disposed when the ChangeNotifier is disposed.
Properties
The lifecycle observer instance.
Methods
initLifecycleObserverListener
void initLifecycleObserverListener()
Initializes the lifecycle observer with callbacks. This method should be called to set up lifecycle observation.
onPaused
Called when the app enters the paused state. Override this method to handle app pause events.
onResumed
Called when the app enters the resumed state. Override this method to handle app resume events.
onInactive
Called when the app enters the inactive state. Override this method to handle app inactive events.
onDetached
Called when the app enters the detached state. Override this method to handle app detached events.
onHidden
Called when the app enters the hidden state. Override this method to handle app hidden events.
Examples
Using LifecycleObserver directly
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late LifecycleObserver lifecycleObserver;
@override
void initState() {
super.initState();
lifecycleObserver = LifecycleObserver(
onResumeCallback: () {
print('App resumed');
},
onPauseCallback: () {
print('App paused');
},
);
WidgetsBinding.instance.addObserver(lifecycleObserver);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(lifecycleObserver);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
Using LifecycleObserverMixin
class MyController with LifecycleObserverMixin {
void init() {
initLifecycleObserver();
}
@override
void onResumed() {
print('App resumed - refresh data');
}
@override
void onPaused() {
print('App paused - save state');
}
void dispose() {
disposeLifecycleObserver();
}
}
Using LifecycleObserverStateMixin
class MyPage extends StatefulWidget {
@override
State<MyPage> createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> with LifecycleObserverStateMixin {
@override
void onResumed() {
print('App resumed');
// Refresh data, restart animations, etc.
}
@override
void onPaused() {
print('App paused');
// Pause animations, save state, etc.
}
@override
void onInactive() {
print('App inactive');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My Page')),
body: Center(child: Text('Content')),
);
}
}
Using LifecycleObserverChangeNotifierMixin
class MyViewModel extends ChangeNotifier with LifecycleObserverChangeNotifierMixin {
bool _isActive = false;
void init() {
initLifecycleObserverListener();
}
@override
void onResumed() {
_isActive = true;
notifyListeners();
}
@override
void onPaused() {
_isActive = false;
notifyListeners();
}
@override
void dispose() {
super.dispose();
}
}
Notes
- The LifecycleObserver is automatically registered and unregistered with WidgetsBinding
- State mixin variant handles initialization and disposal automatically
- All lifecycle callbacks are optional - only override the ones you need
- For ChangeNotifier mixin, call
initLifecycleObserverListener() manually (not automatic like StateMixin)