JakeWharton/ProcessPhoenix

[Question] App with multiple processes.

fengdai opened this issue · 4 comments

I have an application which components are running in two separated processes. Can ProcessPhoenix kill both of the processes at the same time?

What are the two processes? An activity and a service?

Right now we can only kill the process in which the method was called and we can only restart activity processes.

It should be possible to kill all app's processes via BroadcastReceiver which should be started in Application.onCreate() and call Runtime.getRuntime().exit(0) if it catches required intent -> every app's process will catch this broadcast and all of them will be destroyed.

Also, what about scheduling relaunch of the process via AlarmManager with some delay? I am afraid that calling Runtime.getRuntime().exit(0) (BTW, you can use System.exit(0) which calls Runtime.getRuntime.exit()) might not be fast enough and because you are actually starting default activity or some user intent before calling exit(0) and VM can try to launch it before calling exit(0) -> undefined behavior (there is a chance that process will be killed, but new activity won't start, at least I saw such reports in question about app restart on SO).

BTW, you can use System.exit(0) which calls Runtime.getRuntime.exit()

Method overhead is too high. Cannot abide.

I'm using the code below to kill other processes, does it look ok @JakeWharton ? Maybe we could optionally do it in the ProcessPhoenix directly?

/**
 * Wrapper around [ProcessPhoenix] to support multi-process kill
 */
object OSProcessPhoenix {
    /**
     * Kill others processes then call [ProcessPhoenix.triggerRebirth]
     *
     * @see ProcessPhoenix.triggerRebirth
     */
    fun triggerRebirth(context: Context) {
        val am = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val processesInfo = am.runningAppProcesses
        processesInfo?.forEach { process ->
            if (process.pid != Process.myPid()) {
                Process.killProcess(process.pid)
            }
        }

        ProcessPhoenix.triggerRebirth(context)
    }
}