Monika
Monika

Reputation: 74

sharedpreferences sharing between two or more activity

I am having issue in accessing String values from two activities through shared preferences. I think i am making some blunders can any one help me out with it? Basically what i want from activity 1 and activity 2 is to get integer values from user using edit text, store them in shared preferences(restored on application relaunch). now using values of activity 1 and 2 i want to perform calculation in result activity.

my problems:-

  1. how to access shared preferences in result activity?

  2. values are stored in string can i perform calculation on that or do i have to convert them to int in results activity? if yes how to convert them to int (is Integer.valueOff will work?)

Activity 1

public class Abc extends Activity {
    Button one2five, save1;
    EditText edtA, edtB, edtC, edtD, edtE, edtF;
    String tA, tsB, tsC, tsD, tsE, tsF;
    int tB, tC, tD, tE, tF;
    public static String FILE1 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc);
        one2five = (Button) findViewById(R.id.btp1);
        save1 = (Button) findViewById(R.id.btps1);
        edtA = (EditText) findViewById(R.id.etA);
        edtB = (EditText) findViewById(R.id.etB);
        edtC = (EditText) findViewById(R.id.etC);
        edtD = (EditText) findViewById(R.id.etD);
        edtE = (EditText) findViewById(R.id.etE);
        edtF = (EditText) findViewById(R.id.etF);

        abcPref = getSharedPreferences("FILE1", 0);
        edtA.setText(abcPref.getString("tA", ""));
        edtB.setText(abcPref.getString("tsB", ""));
        edtC.setText(abcPref.getString("tsC", ""));
        edtD.setText(abcPref.getString("tsD", ""));
        edtE.setText(abcPref.getString("tsE", ""));
        edtF.setText(abcPref.getString("tsF", ""));

        one2five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((!edtA.getText().toString().equals(""))
                        && (!edtB.getText().toString().equals(""))
                        && (!edtC.getText().toString().equals(""))
                        && (!edtD.getText().toString().equals(""))
                        && (!edtE.getText().toString().equals(""))
                        && (!edtF.getText().toString().equals(""))) {
                    Intent openg2j = new Intent("com.sports.sport.G2J");
                    startActivity(openg2j);
                }
            }
        });
        save1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                abcPref = getSharedPreferences("FILE1", 0);
                SharedPreferences.Editor editor = abcPref.edit();
                editor.putString("tA", edtA.getText().toString());
                editor.putString("tsB", edtB.getText().toString());
                editor.putString("tsC", edtC.getText().toString());
                editor.putString("tsD", edtD.getText().toString());
                editor.putString("tsE", edtE.getText().toString());
                editor.putString("tsF", edtF.getText().toString());
                editor.commit();
            }
        });
    }
}

Activity 2

public class G2J extends Activity {
    Button two2five, save2;

    EditText edtG, edtH, edtI, edtJ, edtK;
    int tG, tH, tI, tJ, tK;
    String tsG, tsH, tsI, tsJ, tsK;
    public static String FileP2 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g2j);
        two2five = (Button) findViewById(R.id.btp2);
        save2 = (Button) findViewById(R.id.btps2);
        edtG = (EditText) findViewById(R.id.etG);
        edtH = (EditText) findViewById(R.id.etH);
        edtI = (EditText) findViewById(R.id.etI);
        edtJ = (EditText) findViewById(R.id.etJ);
        edtK = (EditText) findViewById(R.id.etK);

        abcPref = getSharedPreferences("FileP2", 0);
        edtG.setText(abcPref.getString("tsG", ""));
        edtH.setText(abcPref.getString("tsH", ""));
        edtI.setText(abcPref.getString("tsI", ""));
        edtJ.setText(abcPref.getString("tsJ", ""));
        edtK.setText(abcPref.getString("tsK", ""));

        two2five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((!edtG.getText().toString().equals(""))
                        && (!edtH.getText().toString().equals(""))
                        && (!edtI.getText().toString().equals(""))
                        && (!edtJ.getText().toString().equals(""))
                        && (!edtK.getText().toString().equals(""))) {
                    // TODO Auto-generated method stub
                    Intent openl2p = new Intent("com.sports.sport.Results");
                    startActivity(openl2p);
                }
            }
        });
        save2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                abcPref = G2J.this.getSharedPreferences(FileP2, 0);
                SharedPreferences.Editor editor = abcPref.edit();
                editor.putString("tsG", edtG.getText().toString());
                editor.putString("tsH", edtH.getText().toString());
                editor.putString("tsI", edtI.getText().toString());
                editor.putString("tsJ", edtJ.getText().toString());
                editor.putString("tsK", edtK.getText().toString());
                editor.commit();
            }
        });
    }
}

Finally Results Activity

public class Results extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultmain);



        SharedPreferences abcPref=  this.getSharedPreferences(FILE1,0);

    }
}

Upvotes: 0

Views: 1375

Answers (4)

Tarun
Tarun

Reputation: 13808

In your Abc activiy repalce

abcPref = getSharedPreferences("FILE1", 0);

with

 abcPref = getSharedPreferences(FILE1, 0);

In your G2J activiy repalce

abcPref = getSharedPreferences("FileP2", 0);

with

 abcPref = getSharedPreferences(FileP2, 0);

In Results Activity define

public static String FILE1= "MyPrefsFile";

To perform caluclation on String values you can parse the string like int value = Integer.parseInt(abcPref.getString("tsG")); and so on

If you just want to accept integer values from editText then in your xcm add:

android:inputType="number"

<EditText
android:id="@+id/edtA"
android:inputType="number"
/>

Upvotes: 1

imthegiga
imthegiga

Reputation: 1126

1st you should check for null pointer exception as, if the user saves it then you're committing editor else you're not saving any string values. So if user doesn't save it and still in result activity you're getting string from your preference like abcPref.getString then it will throw a NullPointerException. Also you can perform parsing of String to Integer using Interger.parseInt(abcPref.getString("tA","" + 0)); and so on...

Upvotes: 0

Litrik De Roy
Litrik De Roy

Reputation: 1032

I suggest you use getDefaultSharedPreferences() in both activities instead of getSharedPreferences(). That way there's no risk that you make a typo in the name of the preferences file to be used.

Upvotes: 0

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

try this way
public class Results extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.resultmain);
            SharedPreferences abcPref=  this.getSharedPreferences(FILE1,0);



             int a = Integer.parseInt(abcPref.getString("tA", ""));
             int b = Integer.parseInt(abcPref.getString("tsB", ""));
             int c = Integer.parseInt(abcPref.getString("tsC", ""));
             int d = Integer.parseInt(abcPref.getString("tsD", ""));
             int e = Integer.parseInt(abcPref.getString("tsE", ""));
             int f = Integer.parseInt(abcPref.getString("tsF", ""));

          // Your operations



        }
    }

Upvotes: 0

Related Questions