Skip to content

Commit bdbd6a5

Browse files
authored
Merge pull request #97 from liamhowell13/androidFix
Fix hard crash on Android
2 parents e37683b + a919eab commit bdbd6a5

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

android/src/main/java/com/azesmwayreactnativeunity/ReactNativeUnity.java

+39-37
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,51 @@ public static void createPlayer(final Activity activity, final UnityPlayerCallba
3737
callback.onReady();
3838
return;
3939
}
40-
activity.runOnUiThread(new Runnable() {
41-
@Override
42-
public void run() {
43-
activity.getWindow().setFormat(PixelFormat.RGBA_8888);
44-
int flag = activity.getWindow().getAttributes().flags;
45-
boolean fullScreen = false;
46-
if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
47-
fullScreen = true;
48-
}
49-
50-
unityPlayer = new UnityPlayer(activity, new IUnityPlayerLifecycleEvents() {
51-
@Override
52-
public void onUnityPlayerUnloaded() {
53-
callback.onUnload();
40+
if (activity != null) {
41+
activity.runOnUiThread(new Runnable() {
42+
@Override
43+
public void run() {
44+
activity.getWindow().setFormat(PixelFormat.RGBA_8888);
45+
int flag = activity.getWindow().getAttributes().flags;
46+
boolean fullScreen = false;
47+
if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
48+
fullScreen = true;
5449
}
5550

56-
@Override
57-
public void onUnityPlayerQuitted() {
58-
callback.onQuit();
51+
unityPlayer = new UnityPlayer(activity, new IUnityPlayerLifecycleEvents() {
52+
@Override
53+
public void onUnityPlayerUnloaded() {
54+
callback.onUnload();
55+
}
56+
57+
@Override
58+
public void onUnityPlayerQuitted() {
59+
callback.onQuit();
60+
}
61+
});
62+
63+
try {
64+
// wait a moment. fix unity cannot start when startup.
65+
Thread.sleep(1000);
66+
} catch (Exception e) {
5967
}
60-
});
61-
62-
try {
63-
// wait a moment. fix unity cannot start when startup.
64-
Thread.sleep(1000);
65-
} catch (Exception e) {
66-
}
6768

68-
// start unity
69-
addUnityViewToBackground();
70-
unityPlayer.windowFocusChanged(true);
71-
unityPlayer.requestFocus();
72-
unityPlayer.resume();
69+
// start unity
70+
addUnityViewToBackground();
71+
unityPlayer.windowFocusChanged(true);
72+
unityPlayer.requestFocus();
73+
unityPlayer.resume();
7374

74-
// restore window layout
75-
if (!fullScreen) {
76-
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
77-
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
75+
// restore window layout
76+
if (!fullScreen) {
77+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
78+
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
79+
}
80+
_isUnityReady = true;
81+
callback.onReady();
7882
}
79-
_isUnityReady = true;
80-
callback.onReady();
81-
}
82-
});
83+
});
84+
}
8385
}
8486

8587
public static void pause() {

0 commit comments

Comments
 (0)