startActivityForResult的用法
startActivity(),应该是安卓开发初学者最先接触的一个方法。从一个Activity:A启动跳转到另一个Activity:B,借助启动的Intent能够携带一些参数,数据流向A->B:
val intent = Intent()
//init
resultData.putExtra(FROM, "Quibbler")
startActivity(intent)
1、startActivityForResult方法
通常还会有一些场景,启动完B,返回到A界面。需要返回结果,这时候就要用到startActivityForResult()方法:
startActivityForResult(intent, REQUEST_CODE)
其实startActivity()内部也是调用startActivityForResult()方法,传入的requestCode参数默认是-1,即不需要接收所启动Activity设置的返回结果(即使有的话)。
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
getAutofillClientController().onStartActivity(intent, mIntent);
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
2、setResult设置返回结果
与此同时,被启动的Activity:B要在返回之前调用setResult(int resultCode, Intent data)方法,设置需要传给启动Activity:A的数据,数据载体也是Intent,requestCode参数值不能小于0。
val resultData = Intent() //put some data in this intent
setResult(RESULT_CODE, resultData)
需要注意的是,只有被启动的Activity:B销毁时数据才会返回。且setResult(int resultCode, Intent data)方法的调用要早于finish():
override fun finish() {
val resultData = Intent() //put some data in this intent
//should call before finish()
setResult(RESULT_CODE, resultData)
super.finish()
}
因为在finish(int finishTask)方法中判断当前Activity是否有设置resultData,同时做准备工作,将resultData传递给前一个Activity:A。
/**
* Finishes the current activity and specifies whether to remove the task associated with this
* activity.
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private void finish(int finishTask) {
...
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (false) Log.v(TAG, "Finishing self: token=" + mToken);
if (resultData != null) {
resultData.prepareToLeaveProcess(this);
}
if (ActivityClient.getInstance().finishActivity(mToken, resultCode, resultData,
finishTask)) {
mFinished = true;
...
}
3、onActivityResult返回数据
回到上一个Activity:A中,重写onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)方法。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQUEST_CODE -> {
Log.d(TAG, "resultCode = $resultCode")
//TODO(what to do)
}
...
}
}
onActivityResult()方法回调早于onResume(),可在该回调方法中获取返回的结果,执行具体业务逻辑。
本篇回顾了Android开发中的一个经典方法:startActivityForResult(),这也是安卓开发中最早接触的一个方法。原理值得深入了解,详见后续《startActivityForResult的原理》。
该方法在AndroidX的ComponentActivity类中已经被废弃,推荐使用registerForActivityResult(ActivityResultContract, ActivityResultCallback)注册来获取启动Activity返回的结果,目的是为了将回调结果业务和Activity解耦。详细用法详见《startActivityForResult的替代方法:registerForActivityResult》。
/**
* {@inheritDoc}
*
* @deprecated This method has been deprecated in favor of using the Activity Result API
* which brings increased type safety via an {@link ActivityResultContract} and the prebuilt
* contracts for common intents available in
* {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for
* testing, and allow receiving results in separate, testable classes independent from your
* activity. Use
* {@link #registerForActivityResult(ActivityResultContract, ActivityResultCallback)}
* passing in a {@link StartActivityForResult} object for the {@link ActivityResultContract}.
*/
@Override
@Deprecated
public void startActivityForResult(@NonNull Intent intent,
int requestCode) {
super.startActivityForResult(intent, requestCode);
}