Intent intent = new Intent(MainActivity.class, BActivity.class);
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Log.e("TAG", result.getData().toString());
}).launch(intent);
报错信息:LifecycleOwners must call register before they are STARTED.
java.lang.IllegalStateException: LifecycleOwner com.example.demo.MainActivity@324a655 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
at androidx.activity.result.ActivityResultRegistry.register(ActivityResultRegistry.java:123)
at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:682)
at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:691)
解决方案:
先在onCreate 中声明:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Log.e("TAG", result.getData().toString());
});
}
然后再去跳转的时候调用
mActivityResult.launch(new Intent(MainActivity.class, BActivity.class));