Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using android:process=":remote" recreates android Application object

I'm using AIDL service in my app. I also want to run it another process, so I use android:process=":remote" in service declaration in the manifest.

My problem is that when the :remote process starts it apparently recreates Application object.

I really do not with that as I override application object and call lots of client stuff in the onCreate() method. Yet I want the service code to reside in the same apk with the client.

Can I achieve that? Is Application object always recreated when new process starts?

Appreciate your help. Thanks!

like image 643
ivy_the Avatar asked Mar 21 '13 10:03

ivy_the


2 Answers

I also want to run it another process

Why? What value does that add to the user, to offset the additional RAM, CPU, and battery cost? Very few apps need more than one process.

My problem is that when the ':remote' process starts it apparently recreates Application object

Of course. Each process gets its own.

I really do not with that as I override application object and call lots of client stuff in the 'onCreate()' method

Then get rid of android:process=":remote". Your users will thank you.

Yet I want the service code to reside in the same apk with the client.

What service?

Is Application object always recreated when new process starts?

Yes.

like image 69
CommonsWare Avatar answered Nov 13 '22 01:11

CommonsWare


As already mentioned by CommonsWare, each of the processes gets its own Application object.

In your Application.onCreate() method you can check whether the method is being called from within the main process or from within the remote process and initialize different stuff accordingly.

@Override
public void onCreate()
{
    super.onCreate();

    if(isRemoteProcess(this))
    {
        // initialize remote process stuff here
    }
    else
    {
        // initialize main process stuff here
    }
}

private boolean isRemoteProcess(Context context)
{
    Context applicationContext = context.getApplicationContext();
    long myPid = (long) Process.myPid();
    List<RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) applicationContext.getSystemService("activity")).getRunningAppProcesses();
    if (runningAppProcesses != null && runningAppProcesses.size() != 0)
    {
        for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
        {
            if (((long) runningAppProcessInfo.pid) == myPid && "YOUR_PACKAGE_NAME:remote".equals(runningAppProcessInfo.processName))
            {
                return true;
            }
        }
    }
    return false;
}
like image 2
ra3o.ra3 Avatar answered Nov 13 '22 00:11

ra3o.ra3