Sub topic 7
Recovering from Android Activity Destruction
Assume you know Dart async and await, Flutter StatefulWidget lifecycle methods, and how to add the image_picker package. This chapter focuses on Android behavior when the system destroys your app's activity while another app, such as the gallery, is open.
Why picked data can disappear
image_picker launches a system activity for the gallery or camera. Android may remove your Flutter activity to reclaim memory while the user is choosing a file. When the system activity finishes, Android creates a new activity instead of returning to the original Dart call.
Your original await picker.pickImage(...) call might therefore never receive its result. The plugin temporarily stores the result so that your app can recover it later.
Call `retrieveLostData` during startup
Create one ImagePicker instance and call retrieveLostData when the screen or application starts. Check isEmpty first. If files exist, process them exactly as you would process the normal picker result.
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class RecoverPickerScreen extends StatefulWidget {
const RecoverPickerScreen({super.key});
@override
State<RecoverPickerScreen> createState() => _RecoverPickerScreenState();
}
class _RecoverPickerScreenState extends State<RecoverPickerScreen> {
final ImagePicker _picker = ImagePicker();
XFile? _image;
String? _error;
@override
void initState() {
super.initState();
_recoverLostData();
}
Future<void> _recoverLostData() async {
final LostDataResponse response = await _picker.retrieveLostData();
if (!mounted) return;
if (response.isEmpty) {
return;
}
if (response.files != null && response.files!.isNotEmpty) {
setState(() {
_image = response.files!.first;
});
} else {
setState(() {
_error = response.exception?.toString() ?? 'Could not recover the image';
});
}
}
Future<void> _pickImage() async {
final XFile? image = await _picker.pickImage(
source: ImageSource.gallery,
);
if (!mounted || image == null) return;
setState(() {
_image = image;
_error = null;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Recover a picked image')),
body: Center(
child: _error != null
? Text(_error!)
: _image == null
? const Text('No image selected')
: Image.network(_image!.path),
),
floatingActionButton: FloatingActionButton(
onPressed: _pickImage,
child: const Icon(Icons.photo),
),
);
}
}mounted prevents setState after the widget has been removed. In a mobile-only app, display the recovered XFile with Image.file and dart:io. The example uses Image.network because XFile.path can be a web-compatible URL, but choose the display method for your target platforms.
Handle recovery consistently
Keep normal results and recovered results on the same processing path. For example, pass both to a method that copies the file to permanent storage or uploads it. The recovered file may be temporary, so do not rely on its path remaining available after the process ends.
Call recovery before starting another picker request. If the response contains an exception rather than files, show a retry option and log the exception for device-specific investigation.
Exercise
Why should an app call retrieveLostData after Android starts again?
Show answer
Android can destroy the original Flutter activity while the system picker is open, so the original Dart call may lose its result. retrieveLostData asks the plugin for the saved result.