Disable keep screen on

AndroidFlags

Android Problem Overview


I used:

getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

How do I resume to Default state (no-keep-on)?

Android Solutions


Solution 1 - Android

I think this should do it:

getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

See API for details.

Solution 2 - Android

If you instead set a flag android:keepScreenOn="true" (documentation) only on the views that need to keep the screen on, you wouldn't need to reset the flag manually.

Solution 3 - Android

Another approach

getWindow().setFlags(this.getWindow().getFlags() & ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Also read this

and you can also set android:keepScreenOn="true" in the root View in xml.

Solution 4 - Android

Directly from documentation:

Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags(): getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOkyDokymanView Question on Stackoverflow
Solution 1 - AndroidNoelView Answer on Stackoverflow
Solution 2 - AndroidAlexander AbramovView Answer on Stackoverflow
Solution 3 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 4 - AndroidNazarYavornytskyyView Answer on Stackoverflow