一个activity启动另一个activity最简单的方式是使用startActivity()方法,activity调用startActivity()方法时,实际上是把调用请求发送给操作系统的ActivityManager。ActivityManager负责创建Intent中指定activity的实例并调用其onCreate()方法,应用内部交互图
activity间传递数据则要调用startActivityForResult(…)方法,
FirstActivity:
Intent intent=new Intent(this,Second.class);
intent.putExtra("index",true);
startActivityForResult(intent,0);
@Override
public void startActivityForResult(Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
}
SecondActivity接收数据并调用setResult(…)把处理结果回传给FirstActivity
public class SecondActivity extends AppCompatActivity implements View.OnClickListener{
private TextView answer_tv;
private Button show_answer_tn;
private Boolean answer_cheat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
answer_tv= (TextView) findViewById(R.id.answer_textview);
show_answer_tn= (Button) findViewById(R.id.show_answer_btn); answer_cheat=getIntent().getBooleanExtra("index",false);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.show_answer_btn: answer_tv.setText(answer_cheat.toString().trim());
setAnswerShownResult(true);
break;
}
}
/*setResult()方法应在finish()方法前调用,点击后退键就是调用finish()方法,
最好就是在onCreate()方法中调用,否则会出现还没调用setResult()方法就返回前一个activity
点击后退键默认回传的resultCode为RESULT_CANCELED,表示什么都没做,操作取消
*/
private void setAnswerShownResult(boolean isAnswerShown){
Intent data=new Intent();
data.putExtra("answer_shown",isAnswerShown);
setResult(RESULT_OK,data);
}
}
调用setResult()的时机,查看源码
public final void setResult(int resultCode, Intent data) {
synchronized (this) {
mResultCode = resultCode;
mResultData = data;
}
}
public void finish() {
finish(DONT_FINISH_TASK_WITH_ACTIVITY);
}
private void finish(int finishTask) {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (false) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (resultData != null) {
resultData.prepareToLeaveProcess(this);
}
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData, finishTask)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
可以看出activity返回result是在被finish的时候,也就是说调用setResult()方法必须在finish()之前。
那么如果在如下方法中调用setResult()也有可能不会返回成功: onPause(), onStop(), onDestroy(),
因为这些方法调用不一定是在finish之前的,当然在onCreate()就调用setResult肯定是在finish之前的
按BACK键从一个Activity退出来的,一按BACK,android就会自动调用Activity的finish()方法,然后设置resultCode为RESULT_CANCELED,也就不会返回任何数据了 .
@Override
public void onBackPressed() {
setResult(...);
super.onBackPressed();
}
也可通过重写onBackPressed()方法捕获用户点击后退键的动作,要注意的是setResult(…)要在super.onBackPressed()之前调用
FirstActivity要接收来自SecondActivity返回的数据,重写onActivityResult()方法
//此方法是接收来自其他activity中setResult()传回来的resultCode和intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==0&&resultCode==RESULT_OK){
if (data!=null){
isCheated=data.getBooleanExtra("answer_shown",false);
}
}
}