react-native-background-actions prevent the app from closing by user on Android 10
See original GitHub issueHello and thank you for such a nice library. I faced a problem on Android. When I use background task, and then close my app it doesn’t closes actually despite it is absent in the opened apps list. When I open the app again it starts with the last state that was before closing.
import React, { useEffect } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import SplashScreen from 'react-native-splash-screen';
import BackgroundService from 'react-native-background-actions';
const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));
const veryIntensiveTask = async (taskDataArguments) => {
// Example of an infinite loop task
const { delay } = taskDataArguments;
await new Promise(async (resolve) => {
for (let i = 0; BackgroundService.isRunning(); i++) {
console.log(i);
await sleep(delay);
}
});
};
const options = {
taskName: 'Example',
taskTitle: 'ExampleTask title',
taskDesc: 'ExampleTask description',
taskIcon: {
name: 'ic_launcher',
type: 'mipmap',
},
color: '#ff00ff',
linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info
parameters: {
delay: 1000,
},
};
let i = 0;
export default function App() {
useEffect(() => {
SplashScreen.hide();
}, []);
useEffect(() => {
BackgroundService.start(veryIntensiveTask, options);
return () => {
BackgroundService.stop();
};
}, []);
useEffect(() => {
i++;
console.log({ i });
}, []);
return (
<View style={st.container}>
<Text />
</View>
);
}
const st = StyleSheet.create({
container: { flex: 1 },
});
On first open I see i = 1. Then I close the app and open it again and see i = 2, then i = 3 etc… This leads to several time redux-saga initialization and multiple queries.
“react-native”: “^0.64.2”, “react”: “^17.0.2”, “react-native-background-actions”: “^2.6.5”,
Android 9, 10 BlackView 5900, Xiaomi Redmi Note 7
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
GitHub - Rapsssito/react-native-background-actions
The jobs will run even if the app has been closed. iOS: This library relies on iOS's UIApplication beginBackgroundTaskWithName method, which won't keep...
Read more >react-native-background-actions prevent the app from closing ...
I faced a problem on Android. When I use background task, and then close my app it doesn't closes actually despite it is...
Read more >How can i perform task even if the app is closed in React Native
This library implement's background tasks. It's implementation is in JS and pretty easy to use: ...
Read more >Why do my apps keep crashing on Android, How to fix it
The easiest way to fix an app that keeps crashing on your Android smartphone is to simply force stop it and open it...
Read more >react-native-background-actions - npm
Start using react-native-background-actions in your project by ... that use audio, geolocalization, etc. to keep your app alive in the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alexfov, I am sorry, but background tasks are heavily managed by the OS. Each Android flavor has its own battery management rules and can cause incoherence between devices. If the user closes the app manually from the “open apps” is highly possible that the background task will also be killed.
I don’t know why the app is starting from a previous state, for more info you may take a look at the docs for Headless JS in React Native. Also, make sure your
AndroidManifest.xmlis set toandroid:launchMode="singleTask": https://github.com/Rapsssito/react-native-background-actions/blob/cfbb451813f61ba9ddab71f9486b755bb6f2ed9d/examples/backgroundExample/android/app/src/main/AndroidManifest.xml#L20Ok it’s because BackgroundService doesn’t stopped actually. If you stop it when an app is still opened there is no the issue. It seems like we can’t know and use a callback when a user closes an app