flutter_file_picker icon indicating copy to clipboard operation
flutter_file_picker copied to clipboard

Handle MainActivity destruction on Android (like retrieveLostData of ImagePicker)

Open MilesAdamson opened this issue 1 year ago • 11 comments

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

  • Turn on developer mode
  • Turn "Don't keep activities" on in the developer options (or use a piece of crap Samsung device that is relatively low on memory)
  • Open flutter app
  • Select a file
  • OS now kills the MainActivity
  • When you re-enter your app, whether the screen is restored or not, the selected file is lost

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

MilesAdamson avatar Apr 11 '23 19:04 MilesAdamson

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

philenius avatar Apr 12 '23 21:04 philenius

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

github-actions[bot] avatar Apr 20 '23 01:04 github-actions[bot]

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

MilesAdamson avatar Apr 21 '23 16:04 MilesAdamson

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

khjde1207 avatar May 24 '23 02:05 khjde1207

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

MilesAdamson avatar Jun 09 '23 15:06 MilesAdamson

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

azizarc88 avatar Jul 12 '23 10:07 azizarc88

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

Pluxury avatar Jan 22 '24 11:01 Pluxury

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?

miguelpruivo avatar Mar 20 '24 12:03 miguelpruivo

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

MilesAdamson avatar Apr 04 '24 19:04 MilesAdamson

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.

uzumaki258 avatar Jul 27 '24 15:07 uzumaki258

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

MilesAdamson avatar Aug 16 '24 17:08 MilesAdamson