Nana Kwame
Nana Kwame

Reputation: 1299

How to restart flutter app when it's killed by the OS or User?

I have a Flutter app that I'm running a background service on and it works fine even if the user swipes it away. I see that there're a lot of guides targeting native android implementations of this issue but none for Flutter. I'm using flutter_background_service and it works just fine when the app is in the background but not when it is killed. The app needs to be running all the time to listen to accelerometer events and I've tried a lot of packages like workmanager and flutter_foreground_task but they don't work as I'd like. Workmanager can only run every 15 min but that's too long of a duration and foreground_task although it claims it prevents the app from closing, it doesn't actually do that.

I have no Kotlin experience so using Method channels is not viable at the moment for me,I can copy-paste code though :) and I know some Java. Are there any packages that I'm not aware of that can help me to always listen in the background for events and launch or bring the app to the foreground when I want it to?

Upvotes: 0

Views: 1488

Answers (1)

Janux
Janux

Reputation: 1076

Using the embedder, Flutter code can be integrated into an existing application as a module, or the code may be the entire content of the application. [Flutter architectural overview]

In other words, your Flutter app running on Android is a regular Android app which has the same constraints like any other Android app.

And as mentionend in the README file of workmanager:

Android will automatically change your frequency to 15 min if you have configured a lower frequency.

This statement can also be be found in the Android documentation:

Note: The minimum repeat interval that can be defined is 15 minutes (same as the JobScheduler API.

So, I'd say that you should respect that constraint for your Android app and not try to come up with a hack which works around it.

Upvotes: 1

Related Questions