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);
}
}