6

I use the Navigation Drawer, and in a Fragment that is open I want to have the active screen.

I use:

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

But this thing light up the screen in every fragment of the navigation drawer. How can I get the screen active only in one fragment?

shilovk
  • 11,718
  • 17
  • 75
  • 74

1 Answers1

18

When each fragment is selected from the navigation drawer, for those you want to have the screen kept on do:

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

for those you what to have normal screen dimming and turn-off do:

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Another method for controlling the screen that is more convenient in some cases is View.setKeepScreenOn(). It can be called on any view. It will keep the screen on as long as the view is visible.

You can also control screen-on with the android:keepScreenOn attribute in the layout XML.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158