-1
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!

Kuls
  • 2,047
  • 21
  • 39
user7697691
  • 303
  • 2
  • 9
  • i think you have to use getSharedPreferences instead of getPreferecnes(...) – Nouman Ch Dec 12 '17 at 13:01
  • getPreferences only work for single activity – Nouman Ch Dec 12 '17 at 13:02
  • I dont understand. Why do you use SharedPreferences if you dont want to keep the city when the app restarts? – Ivan Dec 12 '17 at 13:02
  • You've not given any name to your SharedPreference file. Either give name, or use DefaultPreferences. – Danger Dec 12 '17 at 13:02
  • Please fix a little bit your question adding some context before the first piece of code and some lines about how & why the user is changing this value inside shared prefs. – LucioB Dec 12 '17 at 13:15

4 Answers4

0

Initialization of SharedPreference is wrong, Use getSharedPreferences instead of getPreference(), Also give name of your SharedPreference.

Try this out

SharedPreferences sharedPref = getSharedPreferences("PreferenceFile", Context.MODE_PRIVATE);
Kuls
  • 2,047
  • 21
  • 39
0

If you want default city value every time app started, just don't use shared preferences for that. Use loacal variable for city in your MainActivity.

unedim
  • 91
  • 8
0

Either use named file:

SharedPreferences pref = getSharedPreferences("filename", MODE_PRIVATE);

OR use Default file:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Danger
  • 433
  • 5
  • 17
0

In addition to what other users are saying (see for instance @KulsDroid) answer, regardless of how you do read and init your Preferences you should change the way your application starts by adding a custom Application class to your project.

Application class has a method to override called onCreate() that you can use to perform useful things while the app loads and before the user is able to interact. So in your specific case you should invoke something like CityPreferences.setCity(myDefaultCity) inside that onCreate() method.

You can find official doc here and some related SO answers here and here.

Also this is a good tutorial you can use to learn how to build your own Application class.

LucioB
  • 1,744
  • 1
  • 15
  • 32