1

I am instantiating an application object in the manifest file as below.

    <application android:name=".MyApp"
    android:icon="@drawable/add_icon_with_oval"
    android:label="@string/app_name"
    android:process="com.test.myprocess"
    android:allowBackup="false">

I am starting a service from my application which is running in another process. This service is also declared in my manifest file as below.

     <service
        android:name="com.test.package.MyService"
        android:permission="some.permission"
        android:process="com.test.anotherprocess" >
    </service>

The problem that I am seeing here is that MyApp is instantiated twice, one for my process, com.test.myprocess and another instance for com.test.anotherprocess.

Is there is a way that I can prevent creating MyApp for com.test.anotherprocess

1
  • 2
    No because the service needs an Application to run and a single Application can't be shared across different process. Maybe you should just start your service in the same process as your app.
    – pdegand59
    Dec 16, 2015 at 16:21

2 Answers 2

2

Since the service is a component of application, it can't run without an application. The only way to have a single instance of your Application object is to remove the android:process attribute from your service. For more details, check: http://developer.android.com/guide/topics/manifest/service-element.html#proc

1

Is there is a way that I can prevent creating MyApp for com.test.anotherprocess

you cannot, system will will create Application instance for each process. If you have some initialization code in your custom Application class then you can check process name and for example skip it, see code here: Is there a way to get current process name in Android.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.