Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Life Cycle Issue
    primarykey
    data
    text
    <p>I am developing a flashlight application and I want to have it set up to where when the user opens the application, the screen brightness goes to full. I have set up a checkbox preference to do this but have a slight problem. See code below:</p> <pre><code>&lt;CheckBoxPreference android:defaultValue="true" android:key="pref_brightness" android:summary="When checked, brightness will raise to highest level on main activity." android:title="@string/pref_5" /&gt; </code></pre> <blockquote> <p>This is the checkbox code in the xml file for the preferences layout.</p> </blockquote> <pre><code>SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); boolean box = getPrefs.getBoolean("pref_brightness", true); if (box == true) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 100 / 100.0f; getWindow().setAttributes(lp); } </code></pre> <blockquote> <p>And here is where I access the preference inside the MainActivity.java file. This is in the onCreate() method.</p> </blockquote> <p>Now my problem is...when I open the application, the box is checked (unless altered by the user) and the brightness maximizes like it should. However, when I go into the preferences and uncheck the box, then press the "back" button, the box saves but nothing takes effect on the main activity. If I change the box and press the "up" button on the action bar, everything works great. Why is it not working when the back button is pressed? I tried using onBackPressed() but nothing seemed to fix this issue.</p> <h2>**Update according to "Karakuri's" answer:**</h2> <blockquote> <p>I did what you said and it still doesn't work right. If the box is unchecked and I go check it, press the back button, it works but then if I go change it back again, press back again, it doesn't save. Here's the code for your suggestion:</p> </blockquote> <pre><code>@Override public void onWindowFocusChanged(boolean hasFocus) { SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); boolean box = getPrefs.getBoolean("pref_brightness", true); if (box == true) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 100 / 100.0f; getWindow().setAttributes(lp); } super.onWindowFocusChanged(hasFocus); } </code></pre> <p>Help is appreciated,</p> <p>Andrew</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload