简单的说明一下:
通过Start Activity 这个活动启动 Result Data Activity这个活动,并在Result Data Activity这个活动当中获取到UserName后返回给StartActvity这个活动当中;
代码如下:
StartActivity代码如下:
-
@Override
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
switch (requestCode){
-
case 1:
-
if(resultCode==RESULT_OK){
-
String name = data.getStringExtra("name");
-
Toast.makeText(this,name,Toast.LENGTH_LONG).show();
-
}
-
break;
-
default:
-
break;
-
}
-
-
}
-
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.btn_3:
-
// SecondActivity.startAction(this);
-
Intent intent1 = new Intent(this,SecondActivity.class);
-
startActivityForResult(intent1,1);
-
Log.d("barneyx","start_secondactivity!!!!");
-
-
default:
-
break;
-
}
-
}
Result Data Activity代码如下:
-
@Override
-
protected void onCreate(@Nullable Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.second_layout);
-
-
((Button)findViewById(R.id.btn_2)).setOnClickListener(this);
-
}
-
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.btn_2:
-
Intent res_intent = new Intent();
-
String name = ((EditText)findViewById(R.id.name)).getText().toString();
-
Log.d("barneyx",name);
-
res_intent.putExtra("name",name);
-
setResult(RESULT_OK,res_intent);
-
finish();
-
break;
-
default:
-
break;
-
}
-
}
阅读(1088) | 评论(0) | 转发(0) |