可以在Android中的2个不同活动中使用MainActivity的意图吗?

时间:2015-03-04 12:36:38

标签: android android-intent

在我的MainActivity中,我有以下内容

      Intent myintent=new Intent(MainActivity.this, DisplayInformation.class);
            myintent.putExtra("displayName",userName.getText().toString());
            myintent.putExtra("displayContact",userContact.getText().toString());
            myintent.putExtra("displayAddress",userAddress.getText().toString());
            myintent.putExtra("displayStore",userStore.getSelectedItem().toString());
            myintent.putExtra("displayRequest",userRequest.getText().toString());
            startActivity(myintent);

我可以在SecondActivity中正确显示它,这里是代码

        TextView textView1 = (TextView)findViewById(R.id.textView1);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
        TextView textView3 = (TextView)findViewById(R.id.textView3);
        TextView textView4 = (TextView)findViewById(R.id.textView4);
        TextView textView5 = (TextView)findViewById(R.id.textView5);

        textView1.setText(getIntent().getStringExtra("displayName"));
        textView2.setText(getIntent().getStringExtra("displayContact"));
        textView3.setText(getIntent().getStringExtra("displayAddress"));
        textView4.setText(getIntent().getStringExtra("displayStore"));
        textView5.setText(getIntent().getStringExtra("displayRequest"));
Intent myintent=new Intent(DisplayInformation.this, Confirmation.class);
                    myintent.putExtra("confirmID",smsCode.getText().toString());
                    startActivity(myintent);

问题是当我在ThirdActivity中使用它时,它不起作用。

TextView textView2 = (TextView)findViewById(R.id.confirmName);
        TextView textView3 = (TextView)findViewById(R.id.confirmContact);
        TextView textView4 = (TextView)findViewById(R.id.confirmAddress);
        TextView textView5 = (TextView)findViewById(R.id.confirmStore);
        TextView textView6 = (TextView)findViewById(R.id.confirmRequest);

textView2.setText(getIntent().getStringExtra("displayName"));
        textView3.setText(getIntent().getStringExtra("displayContact"));
        textView4.setText(getIntent().getStringExtra("displayAddress"));
        textView5.setText(getIntent().getStringExtra("displayStore"));
        textView6.setText(getIntent().getStringExtra("displayRequest"));

没有任何错误或任何错误。只是它没有显示ThirdActivity中的值。不知道该怎么办?我想在ThirdActivity中显示相同的信息。

2 个答案:

答案 0 :(得分:0)

它没有显示任何内容,因为第二个Intent不包含您想要获得的值。实际上,你只放了" confirmID"第二个意图中的值。如果你还想要其他值,你甚至可以在第二个Intent中设置它们,就像你对第一个那样。

答案 1 :(得分:0)

在将其发送到第三个活动之前,您必须将所有数据再次放入意图中。

Intent myintent = new Intent(DisplayInformation.this, Confirmation.class);
                myintent.putExtra("confirmID", smsCode.getText().toString());
                myintent.putExtra("displayName", textView1.getText().toString());
                myintent.putExtra("displayContact", textView2.getText().toString());
                myintent.putExtra("displayAddress", textView3.getText().toString());
                myintent.putExtra("displayStore", textView4.getSelectedItem().toString());
                myintent.putExtra("displayRequest", textView5.getText().toString());
                startActivity(myintent);