Activity 通信之:传入与返回信息
多Activity 需要建立多个java的class及相对应的xml
建立完成之后必须在manifest中注册
<activity
android:name=".activity2"
android:label="@string/MSSSS" >
</activity>
普通String的传入:
主调程序中:
(将名为将"Seed"以名为Name的String丢给被调用程序)
Button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View V){
Intent intent = new Intent(MainActivity.this, activity2.class);
String name = "Seed";
intent.putExtra("Name", name);
satrtActivity(intent);
}
});
被调程序:
(获取名为Name的String)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Intent intent = this.getIntent();
String message = intent.getStringExtra("Name");
}
传递对象类型
Activity回传数据
1:把名为Name的String丢给被掉程序(用startActivityForResult)
Button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View V){
Intent intent = new Intent(MainActivity.this, activity2.class);
String name = "Seed";
intent.putExtra("Name", name);
startActivityForResult(intent, 1);
}
});
2:在被调Activity中重写finish来设置返回信息
(把名为rtString的String附带在返回intent中,并且回传标记为activity2Return)(用bundle携带)
@Override
public void finish(){
Intent intent = new Intent();
Bundle b = new Bundle();
b.putString("rtString","~~~~");
intent.putExtras(b);
setResult(activity2Return, intent);
super.finish(); www.zzzyk.com
}
3:在主调程序中重载onActivityResult,获得回传的message
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch(resultCode){
case activity2Return:
Bundle b = data.getExtras();
String returnMessage = b.getString("ac2");
}
}
补充:移动开发 , Android ,