/** @deprecated */
@Deprecated
public void startActivityForResult(@SuppressLint({"UnknownNullness"}) Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
}
/** @deprecated */
@Deprecated
public void startActivityForResult(@SuppressLint({"UnknownNullness"}) Intent intent, int requestCode, @Nullable Bundle options) {
super.startActivityForResult(intent, requestCode, options);
}
so 如下修改
/**
* @author juice
* @date 2021/12/16
* @desc 适配 activityResult
*/
public abstract class ResultContract extends ActivityResultContract<Bundle, Intent> {
@NonNull
@Override
public Intent createIntent(@NonNull Context context, Bundle bundle) {
return getIntent(bundle);
}
@Override
public Intent parseResult(int i, @Nullable Intent intent) {
return intent;
}
protected abstract Intent getIntent(Bundle bundle);
}
activity中调用如下
/**
* {@link androidx.activity.ComponentActivity#startActivityForResult(Intent, int)} 弃用后,新的调用方式
* launch 在 activity onCreate() 前初始化
* 需要跳转时 调用{@link ActivityResultLauncher#launch(Object)}
*/
ActivityResultLauncher<Bundle> launcher = registerForActivityResult(new ResultContract() {
@Override
protected Intent getIntent(Bundle bundle) {
Intent intent = new Intent(BaseActivity.this, MainActivity.class);
intent.putExtras(bundle);
return intent;
}
}, new ActivityResultCallback<Intent>() {
@Override
public void onActivityResult(Intent result) {
}
});
启动activity中 setResult 照常
setResult(RESULT_OK, intent)
|