Question

I have the following PreferenceActivity

public class Settings extends PreferenceActivity {

Preference server;
EditTextPreference editServer;
ProgressDialog mDialog;
Handler handler;
Preference bgColor;
EditTextPreference editbgColor;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    server = findPreference("webservice");
    editServer = (EditTextPreference)server;
    bgColor = findPreference("bgColorOption");
    editbgColor = (EditTextPreference)bgColor;

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    saveSettings();
}

private void saveSettings() {
    SharedPreferences settings = getSharedPreferences("settings", 0);
    SharedPreferences.Editor editor = settings.edit();  
    editor.putString("Servidor", String.valueOf(editServer.getText().toString()));
    editor.putString("bgCor",String.valueOf(editbgColor.getText().toString()));
    editor.apply();
}

}

and my settings.xml file is as follows

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory android:title="Sistema"> 

<EditTextPreference
    android:dialogMessage="@string/pref_webservice"
    android:defaultValue="http://"
    android:title="@string/pref_webservice"
    android:key="webservice"/> 

</PreferenceCategory> 

<PreferenceCategory android:title="Aparencia"> 
<EditTextPreference
    android:dialogMessage="@string/BackgroundColorOption"
    android:defaultValue="#ffffff"
    android:title="@string/BackgroundColorOption"
    android:key="bgColorOption" /> 
 </PreferenceCategory> 

My issue is, when i go back from my settings activity i do the saveSettings and return to the main activity, and on my main activity onResume function i apply my new settings, but they dont apply immediatly, if i go on settings a second time and back again , then it does. I belive its because my editor.apply(); takes too long? any way to speed it up?

    @Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    SharedPreferences settings = getSharedPreferences("settings", 0);
    String server = settings.getString("servidor", "xxx");
    String bgColor = settings.getString("bgCor", "#000000");        
    mDialog.setMessage("A Actualizar...");
    thread.setServer(server);
    int cor = Color.parseColor(bgColor);
    mainLayout.setBackgroundColor(cor);
}
Was it helpful?

Solution

I think the problem is that you save your SharedPreferences inside OnStop. Try to move it to the OnPause.

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
saveSettings();
}

Notion from the Android reference:

protected void onStop ()

[...]

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top