Home > Net >  how to make app stop when screen turns off
how to make app stop when screen turns off

Time:08-07

I have created an exercise app with text-to-speech functionality that says exercise name and sound when it's time to rest. but when I turn the screen off app keeps working making the sound and saying the exercise name. I think it has something to do with activity lifecycle, but I am a beginner in android development, so I don't quite know them.

How can I make the app stop working when the screen is off?

CodePudding user response:

You have to pause your TTS manager when your activity/fragment goes to the onPause()/onStop() state and becomes not visible to the end user.

Please, read about activities/fragments lifecycle and determine which state would be the best solution for your case. You can either override these lifecycle methods directly (the easiest way) or you can use the Jetpack Lifecycle lib to create lifecycle-aware components (a cleaner approach if your TTS manager implementations exists as a separate class (and it should be so)).

  • Related