Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Show the video description

In this video, we will learn how to keep the screen activated for a particular activity in our Android app, so it doesn’t automatically turn off after some time of inactivity.
We can do this in 2 ways, either by calling getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) in the activitie’s Java file, or by adding the android:keepScreenOn=”true” attribute to its XML layout file.
This is the alternative for the deprecated PowerManager.FULL_WAKE_LOCK.
With clearFlags we can remove a programmatically added screen-on flag, but even if we don’t remove it, the system will automatically release the flag if we switch to another window, for example when we open another activity or when we put the app into the background.

activity_main.xmlMainActivity.java