UnitySendMessage not working from non-unity activity

Hello, I’m developing an android plugin which should start another activity (with UI and everything…).
This activity (which is not inherits from UnityPlayerActivity of course) should send message to the Unity C# code.
I’m trying to call UnityPlayer.UnitySendMessage, I see my log a moment before sending the message but the C# side doesn’t get it (no exception is raised).

This is the callback which is called from the other activity:

@Override
public void generatePayload() {
   try {
      Log.v(TAG, "generatePayload was triggered");
      UnityPlayer.UnitySendMessage("AndroidObject", "generatePayloadMessage", "");
   } catch (Exception ex) {
      Log.e(TAG, "failed to send message to unity");
   }
}
  • I’ve tried to call this method from the unity primary activity and the message was received successfully in the unity side.
  • I’ve tried also to send the message from the other activity (as I should) but in main thread and it didn’t work too…

Any suggestions? Can’t I send message to unity while UnityActivity isn’t in foreground? If this is the situation - what can I do?

This is expected. Since on Android only one activity is active at a time, starting another activity means that you pause the Unity activity, which in turn pauses the Unity engine. It means that you can’t interact with Unity. You message should be delivered when you get back to the Unity activity (although I haven’t tested it).

In this situation, it depends on what you want to do, but there is a problem of logic if you expect to interact with your game while it is paused (i.e. not the visible / active screen). If you want to show something over the main screen while keeping it active, you need to extend the UnityActivity and show your view as a fragment over it.

Another solution: let’s say you want to make a login dialog to your backend and then send the login result to Unity. You can start your activity and override the onStop() method from this activity (which is called once the activity is finish()'ed). When onStop is called, then the Unity activity is active again, therefore you may send your message from the onStop() method body.