0

How can I access a separate process from MainActivity?

I have embedded a Unity project into a separate Android app, the two are communicating with a java plugin. In order to avoid closure of my entire Android app I am starting my UnityPlayerActivity as a new separate process:

AndroidManifest.xml

     <activity
        android:name="com.mypackage.example.UnityPlayerActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:parentActivityName="com.mypackage.example.MainActivity"
        android:process=":unity_app">
    </activity>

Unfortunately this stops communication between Unity and Android.

Logcat shows the java plugin still receives responses from the unity_app, but I am unable to access this information:

07/07.../com.mypackage.example:unity_app I/In MyPlugin.receiveResult: Result: Score: 45

Any ideas? Possibly because MainActivity loses context of UnityPlayerActivity?

Thanks!

  • You don't get IPC out of the box with the snippet you posted. You'll probably need a `Handler` listening for messages from it. – Shark Jul 09 '18 at 16:21
  • @Shark ah makes sense. Any particular references or examples you recommend? Otherwise I'll follow: http://www.marioalmeida.eu/2014/02/21/how-to-do-android-ipc-using-messenger-to-a-remote-service/ https://developer.android.com/guide/components/bound-services https://developer.android.com/reference/android/app/Service – boogie_bullfrog Jul 09 '18 at 16:43
  • Service might be overkill, but the thing you use will mostly be constrained by Unity, and what it supports (or can use to send messages) – Shark Jul 10 '18 at 13:12
  • 1
    @Shark I implemented a Messenger Service into my [java plugin](https://stackoverflow.com/a/40088914/5811932), which replies to MainActivity with a message updated by Unity on particular events-- unfortunately, the reference disappears when the unity_app is launched: `'void android.os.Messenger.send(android.os.Message)' on a null object reference`. I am correct in thinking that IPC communication should occur between MainActivity and the Java Plugin (which is receiving the unity_app responses)? or does communication have to occur between the UnityPlayerActivity and MainActivity directly? – boogie_bullfrog Jul 10 '18 at 15:15
  • That would depend on what you really want to do, and it would be useful to include a bit more info on your project architecture/structure so that it can be decoupled in the best way. But my guess is that you're just looking for the first "whatever works" kinda solution, so... whatever works for you would do the trick for starts. – Shark Jul 11 '18 at 09:40
  • @Shark Project structure info: my MainActivity launches a Unity project ( [exported as an aar library](https://medium.com/@davidbeloosesky/embedded-unity-within-android-app-7061f4f473a) ) with the click of a button. Once the game wraps up Unity passes some misc game results back to Android -- one way communication is satisfactory, but two way would be ideal. – boogie_bullfrog Jul 11 '18 at 13:35
  • Then the Java plugin should send a message, and `MainActivity` should catch it, a simple `Handler` and `Message` should do the trick then? :) – Shark Jul 11 '18 at 13:38
  • @Shark btw everything works fine when Unity is not started as a separate process. Unfortunately, everything I have read suggests the only way to prevent Unity closure of my entire Android app is to start it as a separate process :( – boogie_bullfrog Jul 11 '18 at 13:45
  • 1
    @Shark Yessir. I am working on that now :D – boogie_bullfrog Jul 11 '18 at 13:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174824/discussion-between-boogie-bullfrog-and-shark). – boogie_bullfrog Jul 11 '18 at 15:52

0 Answers0