Peter Shaw Peter Shaw - 14 days ago 7
Android Question

Include an React-Nativ View into an existing Android App

I have a very simple Android-App, with one view that has a button to an other view.
In that other view i have to show an React-Native component. I followed the instructions on https://facebook.github.io/react-native/docs/integration-with-existing-apps.html#integration-with-existing-apps my Activity is this my tiny code:

public class MyReactActivity extends Activity {

private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mReactRootView = new ReactRootView(this);

mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("main.jsbundle")
.setJSMainModuleName("index.android.js")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactApp", null);

setContentView(mReactRootView);
}


This app starts ok ,but when i touched the button to change the view, the app crashes with:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kris.testa, PID: 2612
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@65e68b3 -- permission denied for window type 2003
at android.view.ViewRootImpl.setView(ViewRootImpl.java:703)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:322)
at com.facebook.react.devsupport.DevSupportManagerImpl.handleReloadJS(DevSupportManagerImpl.java:538)
at com.facebook.react.ReactInstanceManagerImpl$3$1.run(ReactInstanceManagerImpl.java:386)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


What I am doing wrong? Can anyone help?

UPDATE:
My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kris.testa">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyReactActivity" android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

</application>
</manifest>

Answer

Try using this permission in AndroidManifest.

android.permission.SYSTEM_ALERT_WINDOW

on API >= 23 see

Link: http://stackoverflow.com/a/7570071/1796309

If you're using Android version >= 6.0, then you have to follow Android Permission model, i.e. you have to request this permission in runtime.

If you don't want to request permission in runtime, just set your targetSdkVersion to 22 in build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "ru.goenglish.goenglish"
        minSdkVersion 14
        targetSdkVersion 22          <<<<<<<<<<<<<<<<<<<<<<<<<
        versionCode 1
        versionName "1.0"
    }
...
}