Nassef
Nassef

Reputation: 35

updating shared prefrences in android

well i'm just testing the idea of shared preferences to save the user progress, but this simple code is not working, when i pass lev1 it should update preffile so that at next app start it should opens directly to lev2Activity, everything is ok even log cat is clean but nothing happens, i don't know whats wrong with my code, any help will be appreciated.

MainActivity.java

private Button b1;
public static final String levstate= "levstate";
private Context mycontext;

@Override
protected void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mycontext= this;
    b1= (Button) findViewById(R.id.b1);

    b1.setOnClickListener(new View.OnClickListener()

    {

        @Override
        public void onClick(View v) {

            MainActivity.savelevstate(1, mycontext);
            Intent i= new Intent(MainActivity.this, Lev1Activity.class);
            startActivity(i);
        }

    });

}




public static void savelevstate(int state, Context mycontext)

{
    SharedPreferences pref= mycontext.getSharedPreferences("preffile", MODE_APPEND);
    Editor editor= pref.edit();
    editor.putInt("levstate", state);
    editor.commit();
}

public static int getlevstate(Context mycontext)

{
    SharedPreferences pref= mycontext.getSharedPreferences("preffile", MODE_APPEND);
    int state= pref.getInt("levstate", 1);

    return state;


}

Lev1Activity.java

private EditText et1; private Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) 

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lev1);

    et1= (EditText) findViewById(R.id.et1);
    b1= (Button) findViewById(R.id.b1);

}

public void Next (View v)

{

    String value= et1.getText().toString();
    int finalvalue= Integer.parseInt(value);

    if(finalvalue==22)

    {
        Intent i = new Intent (this, Lev2Activity.class);
        startActivity(i);
        MainActivity.savelevstate(2, this);
        this.finish();




    }

}

Upvotes: 1

Views: 85

Answers (4)

Sahar Avr
Sahar Avr

Reputation: 1168

Your idea of using sharedPreferences is excellent. However, if you look at your MainActivity's onCreate(), you can see that you never check the last level state before starting the intent. The app runs, the user clicks on button "b1" and it immediately starts Lev1Activity. Assuming you want the correct level to start when the user presses that same button, you'd have to check for the current level state and then link that state to its appropriate level Activity.

For example (MainActivity.java):

b1.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) {

        Intent i;
        switch(getlevstate(myContext)) {
            case 1:
                i = new Intent(myContext, Lev1Activity.class);
                break;
            case 2:
                i = new Intent(myContext, Lev2Activity.class);
                break;
            case 3:
                i = new Intent(myContext, Lev3Activity.class);
                break;
            case 4
                i = new Intent(myContext, Lev4Activity.class);
                break;
            ...
        }

        startActivity(i);
    }

});

Using MODE_APPEND should work as well as using MODE_PRIVATE, however it is recommended to use the latter.

Upvotes: 3

liltof
liltof

Reputation: 2192

Not sure this is the problem, but when I use Preferences I use Context.MODE_PRIVATE this is the only difference between my code and yours, maybe it will help!

Edit : I might be wrong, but I don't see anywhere the call to getlevstate. After setting this

et1= (EditText) findViewById(R.id.et1);

You shoud do somehing like this :

et1.setText(""+getlevstate(this))

Upvotes: 0

Ashish Tanna
Ashish Tanna

Reputation: 667

You are only saving the states, but you aren't checking it anywhere in the code.

In MainActivity try this :

b1.setOnClickListener(new View.OnClickListener()

{

    @Override
    public void onClick(View v) {
        if(getlevstate(MainActivity.this)==2) {
            Intent i= new Intent(MainActivity.this, Lev2Activity.class);
            startActivity(i);
        } else {
            MainActivity.savelevstate(1, mycontext);
            Intent i= new Intent(MainActivity.this, Lev1Activity.class);
            startActivity(i);
        }
    }

});

Upvotes: 0

burjulius
burjulius

Reputation: 289

I would recommend to you to create a new class for working with SharedPreferences.

Here is a example for saving and retrieving data from shared preferences:

public class SharedPreferenceSettings {

    private static final String PREFS_NAME = "MY_APP_SETTINGS";
    private static final String PREFS_LANGUAGE = "LANGUAGE";
    private static final String PREFS_CITY = "CITY";

    private final SharedPreferences settings;

    public SharedPreferenceSettings(Context context) {
        settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    public void setLanguage(String language) {
        settings.edit().putString(PREFS_LANGUAGE, language).apply();
    }

    public String getlanguage() {
        return settings.getString(PREFS_LANGUAGE, "english");
    }

    public void setCity(String city) {
        settings.edit().putString(PREFS_CITY, city).apply();
    }

    public String getCity() {
        return settings.getString(PREFS_CITY, "london");
    }
}

For more info, check official Android documentation - link

Upvotes: 0

Related Questions