JakeWharton / ProcessPhoenix

Process Phoenix facilitates restarting your application process.
Apache License 2.0
1.98k stars 134 forks source link

[Question] App with multiple processes. #1

Open fengdai opened 9 years ago

fengdai commented 9 years ago

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

JakeWharton commented 9 years ago

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.

artem-zinnatullin commented 9 years ago

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).

JakeWharton commented 9 years ago

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

Method overhead is too high. Cannot abide.

rlatapy-luna commented 5 months ago

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)
    }
}