Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to launch an unregistered ActivityResultLauncher

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?

like image 837
user1242192 Avatar asked Sep 10 '25 21:09

user1242192


2 Answers

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)  
like image 153
Rohaitas Tanoli Avatar answered Sep 12 '25 11:09

Rohaitas Tanoli


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

like image 28
logancodemaker Avatar answered Sep 12 '25 11:09

logancodemaker