Search code examples
androidandroid-edittexttransfer

How to save and transfer the value of more than 5 edit Text from one activity to another?


I want to transfer between activities the data that i edit in my EditText fields. If i transfer from activity 1 to activity 2 i want to be able in activity 2 to edit the data that i received from activity 1, to save the new data in the activity 2 and transfer to the activity 3. How can i save and transfer the data in the same time. Now with shared preferences i can tranfer but i can't save the data that i just edit in the current activity. Please Help! Thank you!

this is the code used to save the weight in Activity 1 (WEEK1):

SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE);
String r1 = w1.getText().toString();
String r2 = w2.getText().toString();
String r3 = w3.getText().toString();
String r4 = w4.getText().toString();
String r5 = w5.getText().toString();
SharedPreferences.Editor editor = WeightPreferences.edit();
editor.putString("wr1", r1);
editor.putString("wr2", r2);
editor.putString("wr3", r3);
editor.putString("wr4", r4);
editor.putString("wr5", r5);
editor.commit();

This is the code used to show the weights from Activity 1 in Activity 2 (WEEK2) and so on:

SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE);
String string1 = weightPreferences.getString("wr1", null);
String string2 = weightPreferences.getString("wr2", null);
String string3 = weightPreferences.getString("wr3", null);
String string4 = weightPreferences.getString("wr4", null);
String string5 = weightPreferences.getString("wr5", null);
w1.setText(string1);
w2.setText(string2);
w3.setText(string3);
w4.setText(string4);
w5.setText(string5);

Solution

  • Put the values into an array and send it like this :

    To send :

    Intent intent =new Intent(ccurrentClass.this, anotherClass.class);
    Bundle b = new Bundle();
    b.putSerializable("value", arrayOfString);
    intent.putExtras(b);
    startActivity(intent);
    

    TO retrieve :

    Intent intent = getIntent();
    Bundle b = intent.getExtras();
    String[][] data = (String[][]) b.getSerializable("value");