miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.36k stars 676 forks source link

Handle MainActivity destruction on Android (like retrieveLostData of ImagePicker) #1258

Open MilesAdamson opened 1 year ago

MilesAdamson commented 1 year ago

Issue

When an Android device is low on memory, or for whatever reason the OS chooses, the MainActivity can be destroyed when the file picker is opened. When the user returns to the app, the file they selected is lost. This is more likely to happen on weaker devices, and with phones that very aggressively kill activities to save battery (Samsung).

It is possible to restore the user to the screen they last were in with flutter's built in RestorationScope, but it's not possible to grab the file they picked. This can put users in a loop where they then try again to pick the same file, MainActivity is killed, the resume where they were without the file picked, and so on. Making it impossible to pick the file.

Related, but they all get automatically closed https://github.com/miguelpruivo/flutter_file_picker/issues/1170 https://github.com/miguelpruivo/flutter_file_picker/issues/733

Steps to reproduce

How this is handled with ImagePicker:

Future<void> getLostData() async {
  final ImagePicker picker = ImagePicker();
  final LostDataResponse response = await picker.retrieveLostData();
  if (response.isEmpty) {
    return;
  }
  final List<XFile>? files = response.files;
  if (files != null) {
    _handleLostFiles(files);
  } else {
    _handleError(response.exception);
  }
}
philenius commented 1 year ago

@MilesAdamson, you're welcome to contribute to this repo. Miguel is too busy :wink:

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 14 days with no activity.

MilesAdamson commented 1 year ago

Sorry I don't think I can add this feature for you

khjde1207 commented 1 year ago

Any workaround? Cannot select file due to Destruction of MainActivity on Galaxy Note 8.

MilesAdamson commented 1 year ago

Not that I know of. Users are not able to pick files on certain tablets in my app either

azizarc88 commented 1 year ago

someone already try to add that feature, but no response anymore https://github.com/miguelpruivo/flutter_file_picker/pull/488

Pluxury commented 10 months ago

Are there any updates on this topic? Why did the author just close this 488 ?

miguelpruivo commented 8 months ago

Sorry — I've been a bit away from the project so I'm trying to catch'all the missing stuff. Is this an issue as of today? Would #488 fix it?

MilesAdamson commented 8 months ago

This is still in the same state as when I created the issue. From what I can tell that PR would solve it yes but I didn't test out his branch

uzumaki258 commented 4 months ago

Handling main activity destruction is hard when the app has more than two functions that call to pick an image. I can not know the retrieveLostData from what function. I have to remove getLostData() from my app.

MilesAdamson commented 3 months ago

Handling main activity destruction is hard when the app has more than two functions that call to pick an image. I can not know the retrieveLostData from what function. I have to remove getLostData() from my app.

I solved that issue in my app by using restoration scope, so the user is brought back to where they were. Then in the init state of where they were, calling retrieveLostData from there, and inside that widget you should know the purpose of why they picked the file and do something with it