skip to Main Content

I can no longer use startActivity using any Intent, even Settings.ACTION_DISPLAY_SETTINGS, because it simply causes the app to crash.

crash

08-06 18:41:05.762 20049 20374 E unknown:ReactNative: Exception in native call
08-06 18:41:05.762 20049 20374 E unknown:ReactNative: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.DISPLAY_SETTINGS pkg="myPackageName" }
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2239)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1878)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Activity.startActivityForResult(Activity.java:5589)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:753)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Activity.startActivityForResult(Activity.java:5547)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:734)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Activity.startActivity(Activity.java:6045)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.app.Activity.startActivity(Activity.java:6012)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at myPackageName.SystemInfo.goToDisplayOptions(SystemInfo.java:149)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at java.lang.reflect.Method.invoke(Native Method)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:146)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at com.facebook.jni.NativeRunnable.run(Native Method)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.os.Handler.handleCallback(Handler.java:958)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.os.Looper.loopOnce(Looper.java:205)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at android.os.Looper.loop(Looper.java:294)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:233)
08-06 18:41:05.762 20049 20374 E unknown:ReactNative:   at java.lang.Thread.run(Thread.java:1012)

I replaced my package name with "myPackageName" for posting here.

It worked before but after the upgrade to Android 14 and React Native 0.74.4, it is no longer working.

I already searched for days for the cause, but I am not seeing any issues causing this behaviour.

Intent i = new Intent();
        i.setPackage(reactContext.getPackageName());
        i.setAction(Settings.ACTION_DISPLAY_SETTINGS);
Objects.requireNonNull(reactContext.getCurrentActivity()).startActivity(i);

2

Answers


  1. Hi please try removing the package name and try to check with below code :

    Intent i = new Intent(Settings.ACTION_DISPLAY_SETTINGS); 
    reactContext.getCurrentActivity().startActivity(i);
    

    No need to specify package name in system setting I think.

    Login or Signup to reply.
  2. Part 1 :

    I do not understand why SDK34 treats the system Intents with defined package name in a way that would make them crash though.

    This happens because if you explicitly define the package name of the intent to your application’s package then the intent can only be resolved by components within your application; this does not include the system settings activities.


    Part 2 :

    Here’s the correct way to create and start the intent to open the display settings:

    Intent i = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
    Objects.requireNonNull(reactContext.getCurrentActivity()).startActivity(i);
    

    Part 3 :

    Safe implementation :

    Intent i = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
    if (i.resolveActivity(reactContext.getPackageManager()) != null) {
        try {
            Objects.requireNonNull(reactContext.getCurrentActivity()).startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Handle the exception, e.g., show an error message to the user
            Log.e("IntentError", "Settings activity not found", e);
        }
    } else {
        // Handle the case where the settings activity is not available
        Log.e("IntentError", "No activity found to handle the intent");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search