Skip to content

How to Get OnDestroyed Method When User Swipe up App #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rohitsangwan01 opened this issue Dec 11, 2021 · 4 comments
Closed

How to Get OnDestroyed Method When User Swipe up App #34

rohitsangwan01 opened this issue Dec 11, 2021 · 4 comments

Comments

@rohitsangwan01
Copy link

Hey , can we have a method which will be called just when app is about to Terminate

@Dev-hwang
Copy link
Owner

You can't get OnDestroyed function if you need to keep foreground service even when app is killed.

@Dev-hwang
Copy link
Owner

You can use this method if you don't mind if the foreground service is killed.

  1. In the service tag of the AndroidManifest.xml file, set the android:stopWithTask option as shown below:
<service
    android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
    android:stopWithTask="true" />
  1. Set the isSticky option to false in AndroidNotificationOptions:
Future<void> _initForegroundTask() async {
    await FlutterForegroundTask.init(
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'notification_channel_id',
        channelName: 'Foreground Notification',
        channelDescription: 'This notification appears when the foreground service is running.',
        channelImportance: NotificationChannelImportance.LOW,
        priority: NotificationPriority.LOW,
        isSticky: false,
      ),
      iosNotificationOptions: const IOSNotificationOptions(),
      foregroundTaskOptions: const ForegroundTaskOptions(
        interval: 5000,
        autoRunOnBoot: true,
        allowWifiLock: true,
      ),
      printDevLog: true,
    );
  }
  1. When the app is killed, TaskHandler's onDestroy function is called.
class FirstTaskHandler extends TaskHandler {
  @override
  Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {

  }

  @override
  Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {

  }

  @override
  Future<void> onDestroy(DateTime timestamp) async {
    // here
  }
}

@rohitsangwan01
Copy link
Author

rohitsangwan01 commented Dec 13, 2021

@Dev-hwang , thanks for reply, i dont need foreground services when user swipe up the app , its just , as long as user using that app , and its still in taskbar, becuase when user swipes up the app with your package , app is not disconnecting with bluetooth(which is connected with a stream) , and becuase of that , when user opens the app , that bluetooth device is still connected , but app as usually opens the scan page( and that bluetooth device will not be discoverable , until i break connection manually from mobile) , so i want this service just to keep bluetooth task in background as long as app is in Taskbar of Mobile , and user can just swipe up and break all connections and stop app services completly , so can i achieve this with your package , will try this above solution , if Ondestroy worked , will close all connection Streams in that

@rohitsangwan01
Copy link
Author

rohitsangwan01 commented Dec 13, 2021

@Dev-hwang , above solution worked perfectly , Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants