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?
Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. You should run any blocking operations on a separate thread within the service to avoid Application Not Responding (ANR) errors.
Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.
Android processes: explained! You should already know by now that Android is based on Linux. As such, each application runs in its own process (with a unique PID): this allows the app to live in an isolated environment, where it cannot be hindered by other applications/processes.
Every Activity in Android is a Process,or One Application is one process.
If service and activity belongs to your app then:
Same process if not defined otherwise. You can create service that will run in separate process.
Service and Activity share same thread. So they can't run simultaneously. But you can create new threads to process commands in Service. Or use IntentService that processes all commands in own thread. Some Service methods always executed on UI thread (e.g. onCreate) Then activity and service can run in parallel (if you have 2 and more cores =).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With