I'm starting an IntentService from MainActivity:onCreate and I noticed this crash from the crash reporting only on Android Oreo above:
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.company.mobile/com.company.mobile.gcm.RegistrationIntentService }: app is in background uid UidRecord{c96fbae u0a184 TPSL idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon()(ContextImpl.java:1577)
at android.app.ContextImpl.startService()(ContextImpl.java:1532)
at android.content.ContextWrapper.startService()(ContextWrapper.java:664)
at cs.a()(FirebaseUtility.java:42)
at com.company.mobile.MainActivity.onCreate()(MainActivity.java:81)
at android.app.Activity.performCreate()(Activity.java:7136)
at android.app.Activity.performCreate()(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate()(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity()(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity()(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute()(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks()(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute()(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage()(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage()(Handler.java:106)
at android.os.Looper.loop()(Looper.java:193)
at android.app.ActivityThread.main()(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke()(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run()(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main()(ZygoteInit.java:858)
Some of you might say this is a duplicate question of this, this, and this. I'm not looking for a how-to answer, rather I'm looking for an explanation why this is happening. I know this issue is because of Background Execution Limits on Android O and I already knew how to fix this by using JobIntentService. I just want to know why this crash is happening in MainActivity:onCreate().
As far as I understand, this issue should only be happening when I'm starting a service while the app is in background, for example after onStop() is called. How could my app even be in the background while MainActivity onCreate is invoked? Are there any scenarios where onCreate is invoked but app is still in background?