从 Activity B 到 Activity A 的额外内容
Extra from Activity B to Activity A
我有两个活动。在 Activity A 中我有 Textview
。单击此按钮后,我使用 EditText
转到 Activity B。
如何将 activity B 中 EditText
的值传回 activity A 中的 TextView
,同时保持 activity A 的原始状态正在处理中?
像这样写ActivityA
public class MainActivity extends Activity {
TextView textView1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
}
代替开始Activity使用startActivityForResult
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
并在 Activity B 中设置这样的结果
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
你可以在 Activity A(onActivityResult
)
中得到 intent.putExtra("MESSAGE",message);
我有两个活动。在 Activity A 中我有 Textview
。单击此按钮后,我使用 EditText
转到 Activity B。
如何将 activity B 中 EditText
的值传回 activity A 中的 TextView
,同时保持 activity A 的原始状态正在处理中?
像这样写ActivityA
public class MainActivity extends Activity {
TextView textView1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
}
代替开始Activity使用startActivityForResult
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
并在 Activity B 中设置这样的结果
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
你可以在 Activity A(onActivityResult
)
intent.putExtra("MESSAGE",message);