Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Within an application, will the activity and service run in the same process?

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?

like image 608
Vamsi Challa Avatar asked Feb 08 '13 08:02

Vamsi Challa


People also ask

Is service a separate process in Android?

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.

What is the difference between activity and service in Android?

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.

Why does Android run each app inside a separate process?

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.

Is an Android activity a process?

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


1 Answers

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 =).

like image 60
Leonidos Avatar answered Oct 01 '22 12:10

Leonidos