Interview Question : With in an Application, will the Activity and Service run in the same process or different processes?
My Answer was ::: In the same process.
Next Question ::: If so, how is that the Activity and Service run at the same time?
My Answer was ::: Operating System will take care of the execution. (Frankly, I didn’t knew the answer).
Can someone give an explanation of the above questions? If my answer was wrong, what is the correct answer?
There is no way to start an Activity and a Service that run in the same process (which is the default behaviour) at the same time.
The onCreate() method of the Service runs on the main (UI) thread.
The onCreate() method of the Activity also runs on the main (UI) thread.
The only way to have both start at more-or-less the same time is to have the Service run in a separate process.
You can try to launch few threads and use semaphore with barier flag.