audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

Is any event fired on app force close (both Android and iOS)

Open subhash279 opened this issue 4 years ago • 6 comments

To which pages does your suggestion apply?

  • ReadMe sections.

Quote the sentences(s) from the documentation to be improved (if any)

Insert here. (Skip if you are proposing an entirely new section.)

I think few things are a bit unclear in the ReadMe on how cleanup can be done when a user force closes the app.

On Android, I think onClose and onTaskRemoved can be used to listen to swiping notification or for force closing the app. I think this can be improved in the documentation.

On iOS, I am not sure myself if it is possible to listen to force closing app event. Can you please let me know which event to listen for on iOS?

subhash279 avatar Apr 26 '21 11:04 subhash279

This issue was automatically closed because it did not follow the issue template.

github-actions[bot] avatar Apr 26 '21 11:04 github-actions[bot]

Reopened.

ryanheise avatar Apr 26 '21 11:04 ryanheise

I'm not sure if this is possible in Flutter because the platform lifecycle events, at least in Android, are synchronous, while platform channels are asynchronous. So when onDestroy fires, it is expected that things are cleaned up synchronously before that method returns. But what we want to do is call through to the Dart layer to allow app-specific cleanup code, but since that executes asynchronously, it will end up happening after onDestroy returns and the OS may kill the process before your Dart code gets a chance to completely run. There's also the case where Android kills your app without even any warning on the native side, e.g. because it urgently needs to reclaim memory. I'm not sure about the iOS side, but probably the situation wouldn't be any better.

ryanheise avatar Apr 26 '21 11:04 ryanheise

@subhash279 is there a use case for this?

nt4f04uNd avatar Apr 28 '21 18:04 nt4f04uNd

Anyways if you want to listen to Activity.onDestroy, usually (if you have a trivial flutter app lifecycle) you can simply do this

import 'package:flutter/material.dart';

void main() {
  final binding = WidgetsFlutterBinding.ensureInitialized();
  binding.addObserver(_WidgetsBindingObserver());
  runApp(App());
}

class _WidgetsBindingObserver extends WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
    if (state == AppLifecycleState.detached) {
      
    }
  }
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      home: Scaffold(
        body: Container(),
      ),
    );
  }
}

nt4f04uNd avatar Apr 28 '21 18:04 nt4f04uNd

@nt4f04uNd when someone force closes the app, it's better to save audio progress to the cloud. Also thanks for the code but I don't know if we can run code after it is detached but I will try.

subhash279 avatar Apr 28 '21 18:04 subhash279