Every Activity in Android is a Process,or One Application is one process

Viewed 18030

In this page, http://androidapps.org.ua/androidintro_ipc.html , intent switching between activities is described as Inter Process Communication. Now I am confused whether every activity is a separate process in android or All activities inside an application is one process. I have checked the process id of all activities and service in my Application using:

 int id = android.os.Process.myPid();
 System.out.println("Process id of Activity1 :"+id);

But it is showing same process id. Please reply back.

4 Answers

You are able to use [multi-processing][1] application approach using Manifest component element with android:process attribute. It is applied for all components

<application>
    <activity/>
    <service/>
    <receiver/>
    <provider/>
</application> 

Additionally <application> element also supports an android:process attribute, to set a default value that applies to all components.

By default a component process name is a package name. It can be overrode by <application> or <android component>(the biggest priority) element

Please note that each app process has is own Application instance. As a result if you define your own custom application class (<application android:name=".CustomApplication" ...>) be ready that at least Application.onCreate() will be called every time during creating a new process.

JFYI, please note that ContentProvider.onCreate() is called before any other inits like Application.onCreate() etc. It can be helpful to get a Context for your library without extra code from dev side

To get process name you can use the next method

@Nullable
public static String getProcessName(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : activityManager.getRunningAppProcesses()) {
        if (processInfo.pid == android.os.Process.myPid()) {
            return processInfo.processName;
        }
    }
    return null;
}

or via Terminal enter adb shell ps | grep <packagename> [1]: https://developer.android.com/guide/components/processes-and-threads#Processes

Related