Tuesday, 3 November 2020

How to run a function, when an Activity gets visible? ( onResume does not work properly)

I would like to start a foreground service, as soon as the Acitvity gets visible. I tried this, by overriding the onResume, but when I compile and run my app, it gets called even if the screen is turned off.

If the screen is turned off, I get IllegalStateException if I try to start a foreground service.

I wrapped the code inside a try-catch block. It mostly works, but sometimes it is not called after the screen gets visible, just right before that.

How is it possible to run a code every time when my Activity is visible by the user?



from How to run a function, when an Activity gets visible? ( onResume does not work properly)

No comments:

Post a Comment