stackUnderflow
stackUnderflow

Reputation: 149

Problems with SharedPreferences

I've been trying to get this to work for a long time, I've checked every question I can find yet as this isn't giving me any errors I don't have much to work from! Basically, I have a button that adds one to an int, which is displayed in a TextView next to it. I'd like this int to stay when the app is closed, so it looks like sharedpreferences is my best option (I also considered sqlite but I'm not certain about whether it can be edited without updating the app, and this just looks easier).

So, my problem is that the int gets increased fine but resets to 0 when I press the back button/close the app etc.

Here's the activity that this is all happening in. EDIT: This is my code so far (with more else ifs)

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ThirdScreenActivity extends Activity {

    public int intPlusOne;
    public int intPlusOne2;
    public static final String PREFS = "MyPrefsFile";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

        //Sets the title to recipe name
        TextView t = ((TextView)findViewById(R.id.textviewPosition));

        Intent intent = getIntent();
        String position = intent.getStringExtra("position");

        t.setText(position);       

        //Sets the correct recipe
    if ("ListView Item 1".equals(position)) {

        TextView t1 = ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string1, null); 

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                intPlusOne++;

            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));            
            }
        });
    }

    else if ("ListView Item 2".equals(position)) {

        TextView t1= ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string2, null); 

        SharedPreferences sharedPrefs = this.getSharedPreferences("PREFS", MODE_PRIVATE);
        intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne2));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            intPlusOne2++;

            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne2));           
            }
        }); 
    }

   public void onPause() {
        super.onPause();

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        Editor editor = sharedPrefs.edit();
        editor.putInt("intPlusOneSaved", intPlusOne);
        editor.commit(); //also tried removing this line
        editor.putInt("intPlusOne2Saved", IntPlusOne2);
        editor.commit();
}   

}

I'd be happy to provide any more details if it helps!

Upvotes: 0

Views: 276

Answers (2)

Brtle
Brtle

Reputation: 2297

The onDestroy is not always called at the end of an activity. You can try to put your code in the onPause method:

public void onPause() {
    super.onPause();

    String PlusOneString = String.valueOf(intPlusOne);
    SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
    Editor editor = sharedPrefs.edit();
    editor.putString("PlusOneSaved", PlusOneString);
    editor.commit();

}

EDIT:

You don't get the saved value in the onCreate method. Try something like this:

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ThirdScreenActivity extends Activity {

    public int intPlusOne;
    public static final String PREFS = "MyPrefsFile";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

    if ("ListView Item 1".equals(position)) {

        TextView tv = ((TextView)findViewById(R.id.TextView1));
        tv.setText(R.string.name, null); 

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        intPlusOne = sharedPrefs.getInt("PlusOneSaved", 0);

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            intPlusOne++;

            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));            
            }
        });
    }

public void onPause() {
        super.onPause();

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        Editor editor = sharedPrefs.edit();
        editor.putInt("PlusOneSaved", intPlusOne);
        editor.commit();

    }   
}

EDIT 2: Put these 2 lines before the first if:

intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0); 

The complete code:

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ThirdScreenActivity extends Activity {

public int intPlusOne;
public int intPlusOne2;
public static final String PREFS = "MyPrefsFile";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    //Sets the title to recipe name
    TextView t = ((TextView)findViewById(R.id.textviewPosition));

    Intent intent = getIntent();
    String position = intent.getStringExtra("position");

    t.setText(position);    

    SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
    intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
    intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0); 

    //Sets the correct recipe
    if ("ListView Item 1".equals(position)) {

        TextView t1 = ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string1, null); 

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                intPlusOne++;

                TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
                PlusOne.setText(String.valueOf(intPlusOne));            
            }
        });
    }

    else if ("ListView Item 2".equals(position)) {

        TextView t1= ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string2, null); 

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne2));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                intPlusOne2++;

                TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
                PlusOne.setText(String.valueOf(intPlusOne2));           
            }
        }); 
    }

    public void onPause() {
        super.onPause();

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        Editor editor = sharedPrefs.edit();
        editor.putInt("intPlusOneSaved", intPlusOne);
        editor.putInt("intPlusOne2Saved", IntPlusOne2);
        editor.commit();
    }
}   

Upvotes: 0

Diljeet
Diljeet

Reputation: 1976

This is simple you made a big mistake.

    String PlusOneString = String.valueOf(intPlusOne);
    Editor editor = sharedPrefs.edit();
    editor.putString("PlusOneSaved", PlusOneString);
    editor.commit();

this code is in onCreate so it saves the value only on application start, when the value is incremented the preferences never got updated

Solution: update the preferences when you increment the value

    SharedPreferences sharedPrefs;//create it outside on create for accessing in on click event


    PlusOneButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        intPlusOne++;

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne));

        /// IMPORTANT PART SAVE THE VALUE TO PREFERENCES
        String PlusOneString = String.valueOf(intPlusOne);
        Editor editor = sharedPrefs.edit();
        editor.putString("PlusOneSaved", PlusOneString);
        editor.commit();

        }});

Upvotes: 1

Related Questions