I am creating a game for Android using LibGDX. On one of my game modes the user uses the Accelerometer to move the player however, the screen will go dim and to sleep since the user is not touching the screen.
I know I can add and clear flags to keep the screen from going to sleep during the entire app:
// Add flags
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Clear flags
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
Using that to keep the app awake works fine but I cant work out how to turn it on (and off) only for a specific screen (which uses the Accelerometer) because you must add the flag in the onCreate() method. Otherwise the screen will not go to sleep when the user is not on the game mode which uses the accelerometer (eg on the Main Menu...).
Basically, I want to be able to programmatically turn on and off the feature to prevent the screen from sleeping. Thanks for any answers!
Note: I don't really want to use a wake lock as it requires special permission and can potentially be dangerous (By not releasing it)
EDIT: I am using libGDX and the screen Interface for my various screens (eg menu, options, game...). As far as I'm aware this is all in only one android view. I want to be able to set "keep_Screen_on" to true in only one of my screens rather than the entire app.