0

I want to use the new value of an edittext preference that is an ip, in the main activity. I don't have to show the value in the main activity. When I try that, I only get the default value until I close and open the app again.

I only know how to save the new value just after be written.

This is the code of the main activity

public String IP;
public int puerto;

//Para crear el menu de settings
@Override
public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;


}

//Se ponen las opciones de los iconos del menu
public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()) {
    case R.id.settings:
        startActivity(new Intent(getApplicationContext(),SettingsActivity.class));
        break;

    case R.id.camera:

        break;

    case R.id.exit:
        this.finish();
        break;

    default:
        break;
    }

    return true;


}





/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    IP = preferences.getString("prefIP", "127.0.0.1");
    puerto = Integer.parseInt(preferences.getString("prefPort", "1101"));

 //After that I use the values to use a socket connection

}

//Preference activity code

 import android.os.Bundle;
 import android.preference.PreferenceActivity;

public class SettingsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);


    }



}
Kydo2
  • 15
  • 3

1 Answers1

0

One way is, in your main activity, override onResume() and use it to check whether the preferences have changed:

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String nuevoIP = preferences.getString("prefIP", "127.0.0.1");
    int nuevoPuerto = Integer.parseInt(preferences.getString("prefPort", "1101"));
    if (puerto != nuevoPuerto || !IP.equals(nuevoIP)) {
        // IP and/or puerto have changed
        IP = nuevoIP;
        puerto = nuevoPuerto;
        // close and reopen socket if appropriate
    }
}

This will get called whenever you return to your main activity from SettingsActivity.

If the shared preferences might change without pausing your main activity, you will have to register an OnSharedPreferenceChangeListener with the shared preferences and do the above logic there. It would then be important to unregister the listener in onPause() and register it in onResume(). You would still need to do the above check in onResume() because while paused it would not have been listening for changes.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521