Where can I find the main loop in android or how can I implement it?
If I for example want to increment x
every time it runs through the activity and test if x
exceeds a value, how would I do that when there is no main loop?

- 555
- 2
- 8
- 23

- 1,343
- 2
- 19
- 33
-
what you mean by main loop? – AADProgramming Jan 25 '15 at 14:55
-
Like a game loop ... It gets executed all the time in a activity, not like onCreate which only gets executed once – Broadwell Jan 25 '15 at 15:04
-
Possible duplicate of [Main loop in Android](http://stackoverflow.com/questions/1099640/main-loop-in-android) or http://stackoverflow.com/questions/14295150/update-activity-constantly – Ciro Santilli OurBigBook.com Feb 03 '16 at 21:54
3 Answers
The main thread runs a Looper
, which is an event-dispatcher (probably what you are referring to as "main loop"). The Looper
is responsible to dispatching events and running small chunks of code. You can't get into the Looper
on every pass, but you can post small chunks of code to the Looper
to get executed, and you can schedule code to be run at a certain time. To do this, you create a Handler
and then use the post()
, postAtTime()
or postDelayed()
methods. You can also queue your own "events" to the Looper
, by posting "messages" to the Handler
. If you do that, you'll need to provide code that actually does something with the messages once they get dispatched.
Be aware that the Looper
on the main thread also dispatches all of the lifecycle events (like onCreate()
, onResume()
, etc.) and all of the UI processing, so that you can't have any long-running operations on it, otherwise your UI will be slow and sluggish and Android may just kill your app due to unresponsiveness. If you need long-running operations you will need to use separate background threads (look at Service
, AsyncTask
or just create your own Thread
.

- 93,459
- 16
- 209
- 274
Activity
in Android app is used to provide user interface. User interact with things like button, check boxes, edit fields to interact with app using Activity.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface
You may need a Service
, which do not supposed to have a UI and is created to perform background work for App.
For example: Play song in Background in Music App.
In Your case, you should create a Service, use a separate thread that do the work of increment value. Check if "x" exceeds value in this thread.

- 555
- 2
- 8
- 23

- 6,077
- 11
- 38
- 58
There is not main loop in android but every time an activity is open it pass threw the on create you can do that a counter is increase every time the onCreate is called for more information about the activity life cycle look here Android Activity

- 139
- 2
- 10
-
There actually is. On the main thread, there's a Looper object. The Lopper is basically a message loop, it waits for messages and dispatches them. This looper is what dispatched lifecycle events, touch events, draw events, etc. Whenever your activity is doing something on the main thread, it's doing it from the Looper. It's also why ui changes aren't drawn immediately- they actually send a redraw event to the looper, and you don't actually draw until the looper processes that mesage. – Gabe Sechan Apr 04 '22 at 15:02