Android Preferences not being reported onCreate after "firstUse" is my mistake, but how?
EDIT I forgot a piece of code. Paste here.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.w("Before switch", (String) item.getTitle());
switch (item.getItemId()) {
case R.id.menu_settings:
Log.w("Before intent","\n");
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
Log.w("back from intent", "\n");
preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
focusAtClue = preferences.getBoolean("focus",true);
screenSaver = preferences.getBoolean("screen",true);
Log.w("after intent", focusAtClue + " " + screenSaver);
return true;
default:
Log.w("onOptItemSel","default");
return super.onOptionsItemSelected(item);
}
}
}
Here's the code onCreate
working to create the settings as shown in Fig. The inserted as a comment xml
in preferences.xml
. It has also been included below.
The settings are saved under the keys focus
and screen
. Right?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
boolean firstUse = preferences.getBoolean(FIRST_USE, true);
if (firstUse){
Toast helloMessage = Toast.makeText(getApplicationContext(), "Hello First Time User",Toast.LENGTH_LONG);
helloMessage.show();
editor = preferences.edit();
editor.putBoolean(FIRST_USE, false);
editor.putBoolean ("focus", false);
/*NOTE THE MATCHED STRING vvvvv
<CheckBoxPreference
android:key="focus" android:title="@string/focusAfterShow" android:summary="Always place the cursor" android:defaultValue="false"/> */
editor.putBoolean ("screen", false);
/*NOTE THE MATCHED STRING vvvvvv
<CheckBoxPreference
android:key="screen" android:title="@string/screenSaver" android:summary="Keep screen on" android:defaultValue="false" /> */
editor.commit();
}
else {
///////////////////// problems here *******************
SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
focusAtClue = preferences.getBoolean("focus",focusAtClue);
screenSaver = preferences.getBoolean("screen",screenSaver);
Log.w("oncreate:", focusAtClue + " " + screenSaver);
///////////////////// problems
}
}
preferences.xml
:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Screen options">
>
<CheckBoxPreference
android:key="focus"
android:title="@string/focusAfterShow"
android:summary="Always place the cursor at the 'clue' (sum) after tapping 'Show'."
android:defaultValue="false"
/>
<CheckBoxPreference
android:key="screen"
android:title="@string/screenSaver"
android:summary="Keep screen on at all times while running this app."
android:defaultValue="false"
/>
</PreferenceCategory>
</PreferenceScreen>
After you left the home screen app and then returned to it, there were no changes to the prefixes, as shown in logcat
.
Then I change the first prefix by checking its field. logcat
shows it correctly. The code used is in SettingsFragment.java
:
package com.bffmedia.hour11app;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.Log;
public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
Log.w("onSharedPrefChgd", sharedPreferences.getBoolean("focus",false) + " " + sharedPreferences.getBoolean("screen",false));
}
}
Then I remove the app from the list of apps in memory and then launch it from the app icon.
onCreate
says both are FALSE, but only the first.
Here logcat
with only lines I wrote through Log.w
. There are 3 groups to look at: creating prefs and changing the first prefix, which worked fine. And then the second onCreate
, where both values are named false
, which is incorrect - initially correct - but the settings screen shows that I checked the first field.
I don't know what I did wrong.
05-16 13:25:20.290 18778-18778/com.bffmedia.hour11app W/Before switch? Settings
05-16 13:25:20.291 18778-18778/com.bffmedia.hour11app W/Before intent? [
05-16 13:25:20.292 438:0x1a07 I/ActivityManager ]
START u0 {cmp=com.bffmedia.hour11app/.SettingsActivity} from pid 18778
05-16 13:25:20.449 18778-18778/com.bffmedia.hour11app W/back from intent?
05-16 13:25:20.450 18778:0x495a W/after intent ]
false false ***************************** CORRECT ************************
05-16 13:35:16.717 18778-18778/com.bffmedia.hour11app W/onSharedPrefChgd?
true false *********************************** CORRECT
05-16 13:37:21.185 20014-20014/com.bffmedia.hour11app W/Before switch? Settings
05-16 13:37:21.186 20014-20014/com.bffmedia.hour11app W/Before intent? [
05-16 13:37:21.188 438:0x2ef I/ActivityManager ]
START u0 {cmp=com.bffmedia.hour11app/.SettingsActivity} from pid 20014
05-16 13:37:21.245 20014-20014/com.bffmedia.hour11app W/back from intent? [
05-16 13:37:21.245 20014:0x4e2e W/after intent ]
false false *************************************** WHY? *******************************************
source to share
The problem is that you are not looking in the XML file for the correct settings. Your settings PreferenceFragment
are saved in the default settings XML file and in your MainActivity as you are using getSharedPreferences(SETTINGS, MODE_PRIVATE)
, which creates a new file in addition to the default preferences XML file.
To fix this, just change this code to PreferenceManager.getDefaultSharedPreferences(this)
and then you will search in the same preference XML file that is using PreferenceFragment
.
More details here .... and also here .....
Here's all you have to do to fix it, I just tested it and it works!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
//use this instead:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstUse = preferences.getBoolean(FIRST_USE, true);
if (firstUse){
Toast helloMessage = Toast.makeText(getApplicationContext(), "Hello First Time User",Toast.LENGTH_LONG);
helloMessage.show();
editor = preferences.edit();
editor.putBoolean(FIRST_USE, false);
editor.putBoolean ("focus", false);
/*NOTE THE MATCHED STRING vvvvv
<CheckBoxPreference
android:key="focus" android:title="@string/focusAfterShow" android:summary="Always place the cursor" android:defaultValue="false"/> */
editor.putBoolean ("screen", false);
/*NOTE THE MATCHED STRING vvvvvv
<CheckBoxPreference
android:key="screen" android:title="@string/screenSaver" android:summary="Keep screen on" android:defaultValue="false" /> */
editor.commit();
}
else {
///////////////////// problems here *******************
//no need to create another preferences, it already defined above
//SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
//This should work now!!!!!!
focusAtClue = preferences.getBoolean("focus",focusAtClue);
screenSaver = preferences.getBoolean("screen",screenSaver);
Log.w("oncreate:", focusAtClue + " " + screenSaver);
Toast helloMessage = Toast.makeText(getApplicationContext(), "Hello Returning User " + focusAtClue + " " + screenSaver,Toast.LENGTH_LONG);
helloMessage.show();
///////////////////// problems
}
}
One more note: it looks like this code below will not behave the way you think. The execution of this code will not pause when called startActivity()
, it will continue. This way, your logs here will reflect the state of the SharedPreference before the Preferences function is opened, not after.
In addition, you should also use the general default preferences.
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
Log.w("back from intent", "\n");
//preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
focusAtClue = preferences.getBoolean("focus",true);
screenSaver = preferences.getBoolean("screen",true);
Log.w("after intent", focusAtClue + " " + screenSaver);
return true;
source to share