public class CityPreference{
SharedPreferences prefs;
public CityPreference(Activity activity) {
prefs = activity.getPreferences(Activity.MODE_PRIVATE);
}
public String getCity(){
String defaultStr = "Helsinki,FI";
return prefs.getString("city", defaultStr);
}
public void setCity(String city){
prefs.edit().putString("city", city).commit();
}
}
MainActivity:
CityPreference cityprefs = new CityPreference(MainActivity.this);
renderWeatherData(cityprefs.getCity());
How I change the String:
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(getResources().getString(R.string.change_city));
final EditText cityInput = new EditText(MainActivity.this);
cityInput.setInputType(InputType.TYPE_CLASS_TEXT);
cityInput.setHint("Helsinki,FI");
builder.setView(cityInput);
builder.setPositiveButton(getResources().getString(R.string.submitBtn), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CityPreference cityPref = new CityPreference(MainActivity.this);
cityPref.setCity(cityInput.getText().toString());
String newCity = cityPref.getCity();
renderWeatherData(newCity);
}
});
builder.show();
}
So, what i want to do is, when everytime I restart the app. It will always put
defaultStr to prefs.getString();
Should I clear the data Storage? or is there any better way to do that? Thanks for helping!