1

Scenario:

  • I have an activity AndroidProcessActivity
  • I launch a new Activity NewProcessActivity from AndroidProcessActivity but this is a different process than previous app
  • Both activities are within te app

Question:

  • Now how to send an event from AndroidProcessActivity to AndroidProcessActivity
  • Is this possible
    <activity
    android:name=".game.NewProcessActivity"
    android:launchMode="singleTask"
    android:process=":testScreen"
    android:theme="@style/AppTheme">

Question is about Inter process communication

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • check https://developer.android.com/reference/android/content/Intent – Diego Torres Milano Mar 09 '23 at 17:14
  • @DiegoTorresMilano .... The question is not about launching a new activity ... its about IPC(interprocess communication) – Devrath Mar 09 '23 at 17:19
  • Are you sure your activities are running on different processes? – Diego Torres Milano Mar 09 '23 at 17:26
  • Did you try BroadcastReceiver? Create one with a custom IntentFilter to catch the event – Tam Huynh Mar 09 '23 at 17:42
  • @DiegoTorresMilano ... Yes it is a different process ... I have added a snippet of the manifest on how it is declared in the manifest – Devrath Mar 10 '23 at 03:04
  • @TamHuynh ... I have tied this in the past it was not working ... let me double check on this – Devrath Mar 10 '23 at 03:04
  • Yes, I think `BroadcastReceiver` works because the event goes through the system by `Intent` and `IntentFilter`, even the communication between the Notification and the current app still uses this process to send the event – Tam Huynh Mar 10 '23 at 04:46
  • @TamHuynh ... I tried this ... it doesn't work ... May be it works if they are two separate apps. I tried my own solution from this article https://medium.com/p/766257cac709#7814 ... it's not working ... U can check once with sample .... I have added the snippet in question on how to declare process in manifest – Devrath Mar 10 '23 at 04:52

1 Answers1

0

Normally a new activity runs in the same process (that is the default). So you can simply call a member function of NewProcessActivity from activity AndroidProcessActivity and vice-versa.

For example you can use a singleton variable inside NewProcessActivity so you have an object when you want to call a member function. But be aware activities can be launched multiple times.

There is the process and multiprocess property of <activity>. Those might result in an activity being run on a separate process.

Because everything (also threads) are called processes on Linux I am not sure if it's possible to simply call methods on this new process from the main process. I recommand to simply try it out and if it works you have to use Java synchronized to make everything thread safe.

If it's not possible (that is what I expect) you have to use IPC:

  • use the start Intent to pass parameters to the activity
  • use broadcasts to send messages in both directions (do this if you have to transfer simple values only)
  • If you have a complicated API you have to create your own service and bind it (see bound services). You have to write an .aidl file. This is how IPC is normally done on Android.
  • If you have native code or you want max. performance you can do native Linux IPC. Those could be local sockets or shared memory for example. But this might require root and it's unusual.
zomega
  • 1,538
  • 8
  • 26