Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FLAG_KEEP_SCREEN_ON Fragment Nav Drawer

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?

like image 465
Debora Carnevali Avatar asked Jul 12 '15 15:07

Debora Carnevali


People also ask

How to add new fragment in navigation drawer in Android studio?

You just create new Fragment class which extend Fragment and their respective XML file and call them in MainActivity eg. Show activity on this post. In android studio 3.5 and above, this comes by default. You will notice different fragment are generated by default.


1 Answers

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.

like image 84
Bob Snyder Avatar answered Sep 27 '22 17:09

Bob Snyder