androidkotlinandroid-activityservicemutablelivedata

After set targetSdk = 34, Activity in background can't receive update from foreground service


I have a Timer app. Use Foreground service to record time duration. This foreground service has a mutablelivedata that will postvalue when the countdown goes to zero. And in MainActiviy I observe this mutablelivedata, if get any update will save the record to the database.

Everything is fine until I set the targetSdk from 33 to 34.

Now if I leave the app to the background, the MainActivity can't get any update from foreground service until I open this activity again. But if targetSdk = 33. I can get the update. So what should I do to migrate from 33 to 34. Thanks.


Solution

  • Personally, I wouldn't have used a MutableLiveData but rather a LocalBroadcast so that you can register from your MainActivity and receive updates from your service.

    And your problem could be a background restriction, but you won't have this problem with the LocalBroadcast.