private ActivityResultLauncher<Intent> launcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
initLaunch();
}
private void initLaunch() {
launcher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
}
});
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.avatar:
intent = new Intent( MainActivity.this, UserInfoActivity.class );
launcher.launch( intent );
break;
}
launcher 要在onCreate内初始化,否则会闪退,之后调用launch方法即可 官方文档点此
|