利用Intent启动activity的例子
1 不带回调的调起和返回
调起
//获取Intent Intent intent = new Intent(this,Main2Activity.class); //获取输入框中的数据 String par = et_par.getText().toString(); //把参数放到inten里面传递 intent.putExtra("par",par); //启动Activity int requestCode =2; startActivityForResult(intent,requestCode);
返回
this.finish();
2 带结果的调起和返回
调起
//获取Intent Intent intent = new Intent(this,Main2Activity.class); //获取输入框中的数据 String par = et_par.getText().toString(); //把参数放到inten里面传递 intent.putExtra("par",par); //启动Activity int requestCode =2; startActivityForResult(intent,requestCode);
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { //super.onActivityResult(requestCode, resultCode, data); if(requestCode==2&&resultCode==3){ String par = data.getStringExtra("par"); et_par.setText(par); } }
返回
int resultCode = 3; Intent data = new Intent(); String val = et_par2.getText().toString(); data.putExtra("par",val); setResult(resultCode,data); this.finish();