Sometimes I'm getting this IllegalStateException, and it says that you must ensure the ActivityResultLauncher is registered before calling launch(). But there is no method to check if ActivityResultLauncher is registered. How can I solve this and why this may happen? Also, it's not clear when to call unregister() method, any examples?
I have been experiencing the same error on all of my Apps on Playstore. After some experiments, I manage to figure out the solution. The problem was "The Launcher gets called when the App leaves the activity". To solve this error, I did only two thing.
STEP # 1: Assigning the launcher as null on Activity Destroy.
@Override
protected void onDestroy() {
super.onDestroy();
someActivityResultLauncher = null;
}
STEP # 2: Adding a Null check before Launching it.
if(someActivityResultLauncher !=null)
someActivityResultLauncher.launch(consentIntent);
Let me explain, My app had 2 activities. Phone number & verification activity. I had a broadcast that receives an OTP message and launched (). If the broadcast has received a message and the user is not on verification activity. The app would crash on Broadcast. Illegal state exception
Exception java.lang.RuntimeException:
at android.app.LoadedApk$ReceiverDispatcher$Args.run (LoadedApk.java:972)
at android.os.Handler.handleCallback (Handler.java:743)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:150)
at android.app.ActivityThread.main (ActivityThread.java:5621)
at java.lang.reflect.Method.invoke
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:684)
Caused by java.lang.IllegalStateException:
at androidx.activity.result.ActivityResultRegistry$2.launch (ActivityResultRegistry.java:168)
at androidx.activity.result.ActivityResultLauncher.launch (ActivityResultLauncher.java:47)
at com.jazz.pakistani.drama.videos.deikho.Activities.Credentials.VerifyPassword$2.onReceive (VerifyPassword.java:289)
at android.app.LoadedApk$ReceiverDispatcher$Args.run (LoadedApk.java:962)
Be aware to register your activityResultLauncher in onCreate method to prevent side-effect
public class MyActiviy extends AppCompatActivity {
.
.
.
private ActivityResultLauncher<Intent> myLauncher;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some stuff
// Register your launcher here
myLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
Intent data = result.getData();
}
});
findViewById(R.id.myButton).setOnClickListener(v -> {
// call `launch` after user click on button or something like that
Intent intent = new Intent(this, TargetActivity.class);
myLauncher.launch(intent);
});
}
}
Refere to Google doc :
When using the ActivityResultRegistry APIs, it's strongly recommended to use the APIs that take a LifecycleOwner, as the LifecycleOwner automatically removes your registered launcher when the Lifecycle is destroyed. However, in cases where a LifecycleOwner is not available, each ActivityResultLauncher class allows you to manually call unregister() as an alternative.
Take a look at Here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With