1

I have two Activities (A and B). Activity A consists of two EditText (1 and 2). I have an intent to launch Activity B from A. Then, from Activity B, I have an intent to pass a string to EditText2.

My problem now is, after passing an intent from Activity B to EditText2, the word that I typed in EditText1 disappears. My question is, how can I maintain a word that I typed in EditText1 even after passing an intent from Activity B to EditText2. Here's the cycle of my problem:

Typed something in EditText1 > Launch Activity B > get string from Activity B and pass it in EditText2 > typed word in EditText1 disappears

Here's my code:

Activity A:

public class ActivityA extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        Intent intent = getIntent();
        String message = intent.getStringExtra(ActivityB.EXTRA_MESSAGE);

        EditText editText = (EditText) findViewById(R.id.edittext2);
        editText.setText(message);   
    }

    public void to_actb(View view) {

        Intent intent = new Intent(this, ActivityB.class);
        startActivity(intent);
    }

Activity B

public class ActivityB extends Activity {

    public final static String EXTRA_MESSAGE = "hello.hey.MESSAGE";

....

    public void to_acta(View view) {

        Intent intent = new Intent(this, ActivityA.class);
        TextView textView = (TextView) findViewById(R.id.text);
        String message = textView.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);      
    }
general_bearbrand
  • 827
  • 5
  • 13
  • 29

1 Answers1

0

Using this structure, what you are doing is:

Call ActivityA(ID:1 for example)-> Call ActivityB(ID:2)-> Call ActivityA( ID 3!!) You are not calling the same activity as the first time, you are getting a new ActivityA with its own values.

Best way to fix this is to use the startActivityforResult, passing data in a return-intent. Here you can find a good example https://stackoverflow.com/a/10407371/585540.

Community
  • 1
  • 1
Neonamu
  • 736
  • 1
  • 7
  • 21