0

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);      
    }
4

1 回答 1

0

使用这种结构,您正在做的是:

调用 ActivityA(例如 ID:1)-> 调用 ActivityB(ID:2)-> 调用 ActivityA(ID 3 !!)您没有调用与第一次相同的活动,您将获得一个具有自己值的新 ActivityA .

解决此问题的最佳方法是使用 startActivityforResult,以返回意图传递数据。在这里你可以找到一个很好的例子https://stackoverflow.com/a/10407371/585540

于 2013-11-10T19:02:38.057 回答