当前位置:   article > 正文

LifecycleOwner com.xx.MainActivity@28b4e79 is attempting to register while current state is RESUMED.

is attempting to register while current state is resumed.
Android api29中startActivityForResult提示已过时,应该变成registerForActivityResult,当替换成这个方法的时候,在跳转的时候(Activity的onStart方法之后)执行以下代码,
  1. ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(
  2. new ActivityResultContracts.StartActivityForResult(),
  3. result -> {
  4. if (result.getResultCode() == RESULT_OK) {
  5. //获取返回的结果
  6. String data = result.getData().getStringExtra("data");
  7. Toast.makeText(activity, data, Toast.LENGTH_SHORT).show();
  8. }
  9. });
  10. Intent intent = new Intent(JumpPage.this, NewWordActivity.class);
  11. intent.putExtra("userId", "xxx");
  12. 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.)。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/231689
推荐阅读
  

闽ICP备14008679号