Skip to content
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

java.lang.IllegalStateException: Tried to start task BackgroundTask while in foreground, but this is not allowed #65

Open
nicatasa opened this issue Dec 7, 2018 · 6 comments

Comments

@nicatasa
Copy link

nicatasa commented Dec 7, 2018

Phone: Xiaomi redmi note 4x

Tried to start task BackgroundTask while in foreground, but this is not allowed
at (Headless.JsTaskContext.java: 92)
at(Headless.JsTaskService.java: 125)
and etc...

How to fix it ?

BackgroundTask.define(() => { GetCountryNames(); BackgroundTask.finish() }) BackgroundTask.schedule({ period: 10800 })

@nicatasa
Copy link
Author

nicatasa commented Dec 7, 2018

This error happens every 3hours (period: 108000) only xiaomi devices or only my device for some background restrics and permissions.

@0xAnakin
Copy link

0xAnakin commented Jan 16, 2019

java.lang.IllegalStateException:
at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask (HeadlessJsTaskContext.java:92)
at com.facebook.react.HeadlessJsTaskService$2.run (HeadlessJsTaskService.java:125)
at android.os.Handler.handleCallback (Handler.java:790)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:171)
at android.app.ActivityThread.main (ActivityThread.java:6656)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)

Any update on this? the error occurs in multiple Android versions 7-9 and others

@JaydeepJikadra
Copy link

Facing the Same Issue.

OS Version: 9

Device: ONEPLUS A6010

RAM Free: 28.8%

Disk Free: 52.2%

#0. Crashed: main
at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:92)
at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:125)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

--

Fatal Exception: java.lang.IllegalStateException: Tried to start task BackgroundTask while in the foreground, but this is not allowed.
at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:92)
at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:125)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

@sefam-vbastien
Copy link

It seems this is a behavior wanted by react native as stated here :
https://facebook.github.io/react-native/docs/headless-js-android

By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. You can pass a fourth boolean argument to control this behaviour.

Until you set this boolean to true this will crash.

The library does not set this boolean which means it is set to default (false)

To fix this issue you have 2 choices:
Either you set the boolean to true but this will slow your UI if in foreground mode
Either you can do the isAppOnForeground check as state in the official documentation.

Hope this help.

@qlerebours
Copy link

I tried to set this boolean to true and it seems to be working fine.
Thank you

@vahidrn98
Copy link

i was facing this issue even after setting foreground attribute to true.
then i found out i was stupidly setting a periodic job with setInterval() function inside the task.
deleting that solved my problem.

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

6 participants