Skip to content

When running on Android, error: Mini App cannot be cast to androidx.fragment.app.FragmentActivity #1363

@marc-hughes

Description

@marc-hughes

I am unable to get a mini app running inside an external android project, and consistently get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rosettastone.testapp, PID: 29470
    java.lang.ClassCastException: com.walmartlabs.ern.container.miniapps.TestMiniAppActivity cannot be cast to androidx.fragment.app.FragmentActivity
        at com.facebook.react.modules.dialog.DialogModule.getFragmentManagerHelper(DialogModule.java:245)
        at com.facebook.react.modules.dialog.DialogModule.onHostResume(DialogModule.java:177)
        at com.facebook.react.bridge.ReactContext$1.run(ReactContext.java:174)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

To reproduce:

  1. Start a new ern mini app ern create-miniapp test
  2. Create a new android project in android studio, selecting "Basic Activity" and the defaults from there on.
  3. Run ern run-android, verify the application correctly launches
  4. Run the android project from android studio, verify it launches
  5. Publish the mini app ern publish-container --platform android -p ern-container-publisher-maven -e publish.json
  6. Integrate the mini app into the android app (see https://github.com/marc-hughes/electrode-native-fragment-bug-report for full code)
  7. Run the android app, get the error above.

The cause appears to be that react native's DialogModule attempts to do this:

  private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
    Activity activity = getCurrentActivity();
    if (activity == null) {
      return null;
    }
    return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
  }

And the mini app does not inherit from FragmentActivity.

It looks like this would have been an issue since facebook/react-native#23365

ern --version
 ___ _        _               _       _  _      _   _
| __| |___ __| |_ _ _ ___  __| |___  | \| |__ _| |_(_)_ _____
| _|| / -_) _|  _| '_/ _ \/ _` / -_) | .` / _` |  _| \ V / -_)
|___|_\___\__|\__|_| \___/\__,_\___| |_|\_\__,_|\__|_|\_/\___|
[v0.38.2] [Cauldron: tutoring-cauldron]

ℹ ern-local-cli : 0.38.2
ℹ electrode-native : 1.0.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions