赞
踩
Android api29中startActivityForResult提示已过时,应该变成registerForActivityResult,当替换成这个方法的时候,在跳转的时候(Activity的onStart方法之后)执行以下代码,
- ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(
- new ActivityResultContracts.StartActivityForResult(),
- result -> {
- if (result.getResultCode() == RESULT_OK) {
- //获取返回的结果
- String data = result.getData().getStringExtra("data");
- Toast.makeText(activity, data, Toast.LENGTH_SHORT).show();
- }
- });
-
- Intent intent = new Intent(JumpPage.this, NewWordActivity.class);
- intent.putExtra("userId", "xxx");
- intentActivityResultLauncher.launch(intent);
然后就会报错(LifecycleOwner com.xx.MainActivity@28b4e79 is attempting to register while current state is RESUMED.)。
解决方法:这个registerForActivityResult方法要写在Activity的onCreate方法里(在Activity创建的时候就要创建出来,不能等到使用的时候再创建,不然会报错LifecycleOwner com.xx.MainActivity@28b4e79 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.)。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。