flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

Cant relaunch unity widget after went to background

Open virtyos opened this issue 2 years ago • 6 comments

After my app was gone to background and launched again unity widget had stopped to render

Getting this error in logs

2022-07-25 05:49:06.191 13751-13845/? W/OpenGLRenderer: Surface doesn't have any previously queued frames, nothing to readback from
2022-07-25 05:49:06.209 13751-13751/? D/FlutterUnityController: onDestroy
2022-07-25 05:49:06.209 13751-13751/? D/FlutterUnityController: onDestroy
2022-07-25 05:49:06.212 13751-13751/? I/CustomUnityPlayer: onDetachedFromWindow
2022-07-25 05:49:06.216 13751-13751/? W/FlutterSurfaceView: detachFromRenderer() invoked when no FlutterRenderer was attached.
2022-07-25 05:49:06.217 13751-13751/? D/FUWPlugin: onDetachedFromActivity
2022-07-25 05:49:06.218 13751-13751/? D/FUWPlugin: handleActivityChange
2022-07-25 05:49:06.220 13751-13751/? E/UnityPlayerUtils: An operation is not implemented: Not yet implemented
2022-07-25 05:49:06.220 13751-13751/? D/FUWPlugin: onDetachedFromEngine
2022-07-25 05:49:06.221 13751-13751/? D/FlutterUnityController: this controller disposed
2022-07-25 05:49:06.221 13751-13751/? D/FlutterUnityController: Attaching unity to view
2022-07-25 05:49:06.228 13751-13751/? E/PlatformViewsController: Disposing platform view threw an exception
    java.lang.NullPointerException
        at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetPlugin$onAttachedToEngine$1.getLifecycle(FlutterUnityWidgetPlugin.kt:35)
        at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetController.dispose(FlutterUnityWidgetController.kt:101)
        at io.flutter.plugin.platform.PlatformViewsController$1.dispose(PlatformViewsController.java:376)
        at io.flutter.plugin.platform.PlatformViewsController.diposeAllViews(PlatformViewsController.java:968)
        at io.flutter.plugin.platform.PlatformViewsController.onDetachedFromJNI(PlatformViewsController.java:860)
        at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:428)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:708)
        at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:715)
        at android.app.Activity.performDestroy(Activity.java:8625)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1364)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5706)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5752)
        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:233)
        at android.os.Looper.loop(Looper.java:334)
        at android.app.ActivityThread.main(ActivityThread.java:8333)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
2022-07-25 05:49:06.241 13751-13857/? E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
2022-07-25 05:49:06.262 13751-13751/? W/FlutterJNI: Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: plugin.xraph.com/unity_view_0. Response ID: 10
2022-07-25 05:49:07.406 13751-13751/? D/FUWPlugin: onAttachedToEngine
2022-07-25 05:49:07.406 13751-13751/? D/FUWPlugin: onAttachedToActivity
2022-07-25 05:49:07.406 13751-13751/? D/FUWPlugin: handleActivityChange

android 11, flutter 3.0.5, unity 2022, fuw-2022.1.7f1.unitypackage

virtyos avatar Jul 25 '22 02:07 virtyos

Did you try:

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    case AppLifecycleState.resumed:
      unityWidgetController.resume();
      break;
    case AppLifecycleState.paused:
      unityWidgetController.pause();
      break;
    default:
  }
}

For more read Flutter's: WidgetsBindingObserver

Ahmadre avatar Jul 25 '22 14:07 Ahmadre

@Ahmadre not working, result is the same.

my logs while running:

I/flutter (31765): STATE AppLifecycleState.inactive
I/flutter (31765): STATE AppLifecycleState.paused
D/FlutterUnityController(31765): onDestroy
D/FlutterUnityController(31765): onDestroy
D/FlutterUnityController(31765): onDestroy
I/CustomUnityPlayer(31765): onDetachedFromWindow
W/FlutterSurfaceView(31765): detachFromRenderer() invoked when no FlutterRenderer was attached.
D/FUWPlugin(31765): onDetachedFromActivity
D/FUWPlugin(31765): handleActivityChange
W/MessageQueue(31765): Handler (android.os.Handler) {e1928f9} sending message to a Handler on a dead thread
W/MessageQueue(31765): java.lang.IllegalStateException: Handler (android.os.Handler) {e1928f9} sending message to a Handler on a dead thread
W/MessageQueue(31765): 	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:564)
W/MessageQueue(31765): 	at android.os.Handler.enqueueMessage(Handler.java:778)
W/MessageQueue(31765): 	at android.os.Handler.sendMessageAtTime(Handler.java:727)
W/MessageQueue(31765): 	at android.os.Handler.sendMessageDelayed(Handler.java:697)
W/MessageQueue(31765): 	at android.os.Handler.sendMessage(Handler.java:635)
W/MessageQueue(31765): 	at android.os.Message.sendToTarget(Message.java:471)
W/MessageQueue(31765): 	at com.unity3d.player.UnityPlayer$e.a(Unknown Source:10)
W/MessageQueue(31765): 	at com.unity3d.player.UnityPlayer$e.a(Unknown Source:2)
W/MessageQueue(31765): 	at com.unity3d.player.UnityPlayer.destroy(Unknown Source:53)
W/MessageQueue(31765): 	at com.unity3d.player.UnityPlayer.quit(Unknown Source:0)
W/MessageQueue(31765): 	at com.xraph.plugin.flutter_unity_widget.UnityPlayerUtils$Companion.quitPlayer(UnityPlayerUtils.kt:124)
W/MessageQueue(31765): 	at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetPlugin.handleActivityChange(FlutterUnityWidgetPlugin.kt:86)
W/MessageQueue(31765): 	at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetPlugin.onDetachedFromActivity(FlutterUnityWidgetPlugin.kt:69)
W/MessageQueue(31765): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.detachFromActivity(FlutterEngineConnectionRegistry.java:383)
W/MessageQueue(31765): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:690)
W/MessageQueue(31765): 	at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:715)
W/MessageQueue(31765): 	at android.app.Activity.performDestroy(Activity.java:8625)
W/MessageQueue(31765): 	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1364)
W/MessageQueue(31765): 	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5706)
W/MessageQueue(31765): 	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5752)
W/MessageQueue(31765): 	at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
W/MessageQueue(31765): 	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
W/MessageQueue(31765): 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
W/MessageQueue(31765): 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
W/MessageQueue(31765): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
W/MessageQueue(31765): 	at android.os.Handler.dispatchMessage(Handler.java:106)
W/MessageQueue(31765): 	at android.os.Looper.loopOnce(Looper.java:233)
W/MessageQueue(31765): 	at android.os.Looper.loop(Looper.java:334)
W/MessageQueue(31765): 	at android.app.ActivityThread.main(ActivityThread.java:8333)
W/MessageQueue(31765): 	at java.lang.reflect.Method.invoke(Native Method)
W/MessageQueue(31765): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
W/MessageQueue(31765): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
E/UnityPlayerUtils(31765): An operation is not implemented: Not yet implemented
D/FUWPlugin(31765): onDetachedFromEngine
I/flutter (31765): STATE AppLifecycleState.detached
D/FlutterUnityController(31765): this controller disposed
D/FlutterUnityController(31765): Attaching unity to view
E/PlatformViewsController(31765): Disposing platform view threw an exception
E/PlatformViewsController(31765): java.lang.NullPointerException
E/PlatformViewsController(31765): 	at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetPlugin$onAttachedToEngine$1.getLifecycle(FlutterUnityWidgetPlugin.kt:35)
E/PlatformViewsController(31765): 	at com.xraph.plugin.flutter_unity_widget.FlutterUnityWidgetController.dispose(FlutterUnityWidgetController.kt:101)
E/PlatformViewsController(31765): 	at io.flutter.plugin.platform.PlatformViewsController$1.dispose(PlatformViewsController.java:376)
E/PlatformViewsController(31765): 	at io.flutter.plugin.platform.PlatformViewsController.diposeAllViews(PlatformViewsController.java:968)
E/PlatformViewsController(31765): 	at io.flutter.plugin.platform.PlatformViewsController.onDetachedFromJNI(PlatformViewsController.java:860)
E/PlatformViewsController(31765): 	at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:428)
E/PlatformViewsController(31765): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:708)
E/PlatformViewsController(31765): 	at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:715)
E/PlatformViewsController(31765): 	at android.app.Activity.performDestroy(Activity.java:8625)
E/PlatformViewsController(31765): 	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1364)
E/PlatformViewsController(31765): 	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5706)
E/PlatformViewsController(31765): 	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5752)
E/PlatformViewsController(31765): 	at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
E/PlatformViewsController(31765): 	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
E/PlatformViewsController(31765): 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
E/PlatformViewsController(31765): 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
E/PlatformViewsController(31765): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
E/PlatformViewsController(31765): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/PlatformViewsController(31765): 	at android.os.Looper.loopOnce(Looper.java:233)
E/PlatformViewsController(31765): 	at android.os.Looper.loop(Looper.java:334)
E/PlatformViewsController(31765): 	at android.app.ActivityThread.main(ActivityThread.java:8333)
E/PlatformViewsController(31765): 	at java.lang.reflect.Method.invoke(Native Method)
E/PlatformViewsController(31765): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
E/PlatformViewsController(31765): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
W/FlutterJNI(31765): Tried to send a platform message response, but FlutterJNI was detached from native C++. Could not send. Response ID: 30
W/FlutterJNI(31765): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: plugin.xraph.com/unity_view_0. Response ID: 11

virtyos avatar Jul 26 '22 01:07 virtyos

the issue takes place when the app goes to backgroud after pushing back button. As I understand we need to catch onDestroy event? But I dont know how.

virtyos avatar Jul 26 '22 01:07 virtyos

Actually the problem is that it's imp[ossible to recreate unity widget (or controller?) after calling ondestroy


D/FlutterUnityController(23019): onDestroy
D/FlutterUnityController(23019): onDestroy

virtyos avatar Jul 26 '22 02:07 virtyos

any updates?

virtyos avatar Jul 27 '22 15:07 virtyos

Hi @virtyos, Can you show yours flutter UnityWidget code? Its seems you close player after calls onDestroy then its no possible to recreate playerAgain. Because unityplayer need to works in background.

dawiddszewczyk avatar Aug 22 '22 20:08 dawiddszewczyk

I'm having the same problem.

https://user-images.githubusercontent.com/72723485/231081303-88bc60dc-5799-4fbf-9489-1dfdbd3e2399.mp4

swkim-lbstech avatar Apr 11 '23 07:04 swkim-lbstech