A guide to Flutter for Android developers: Activities and Fragments
What are the corresponding concepts of Activity and Fragment in Flutter?
In Android, an Activity represents a single task that a user can complete. A Fragment represents a behavior or part of the user interface. Fragment is used to modularize your code, assemble complex user interfaces for larger screens, and adapt the interface of your application. In Flutter, both of these concepts correspond to widgets.
To learn more about creating an Activity and Fragment interface, read the Community’s Medium article, a Guide to Flutter for Android developers: How to Design an Activity interface in Flutter.
As mentioned in the Intents section, the interface in a Flutter is represented as a Widget because everything in a Flutter is a Widget. You use Navigator to navigate between routes representing different screens or pages, or just different states and renders of the same data.
How do I listen for Android Activity lifecycle events?
In Android, you can overwrite Actvity lifecycle methods to monitor its life cycle, also can register ActivityLifecycleCallbacks on the Application. In a Flutter, the two methods all have no, but you can through the binding WidgetsBinding observer and listening didChangeAppLifecycleState () change events to monitor lifecycle.
Life cycle events that can be observed are:
-
Inactive – The application is inactive and does not receive user input. This event only applies to iOS, there is no corresponding event on Android.
-
Paused – The application is currently invisible to the user, cannot respond to user input, and is running in the background. This event corresponds to onPause() in Android;
-
Resumed – An application is visible to the user and can respond to user input. This event corresponds to onPostResume() in Android;
-
Kickbacks – applications that are temporarily suspended. This event corresponds to onStop in Android; This event is not triggered on iOS because there is no corresponding event.
For more details on what these states mean, check out the AppLifecycleStatus documentation.
You may have noticed that only a small number of Activity lifecycle events are available; Although FlutterActivity internally captures almost all Activity life cycle events and sends them to the Flutter engine, they are mostly shielded from you. Flutter manages the start and stop of your engine for you, and in most cases there is little reason to listen to the Activity lifecycle on one side of the Flutter. If you need to listen to the lifecycle to acquire or release native resources, you should do it on the native side anyway.
The following example shows how to listen for the lifecycle state of the container Activity:
import 'package:flutter/widgets.dart';
class LifecycleWatcher extends StatefulWidget {
@override
_LifecycleWatcherState createState() => _LifecycleWatcherState();
}
class _LifecycleWatcherState extends State<LifecycleWatcher> with WidgetsBindingObserver {
AppLifecycleState _lastLifecycleState;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() {
_lastLifecycleState = state;
});
}
@override
Widget build(BuildContext context) {
if (_lastLifecycleState == null)
return Text('This widget has not observed any lifecycle changes.', textDirection: TextDirection.ltr);
return Text('The most recent lifecycle state this widget observed was: $_lastLifecycleState. ', textDirection: TextDirection.ltr); }}void main() {
runApp(Center(child: LifecycleWatcher()));
}
Copy the code