Is there a function in Android analogous to "int main" in C/C++ which contains the program's main loop?
Asked Answered
S

5

12

Normally in a C or C++ program there's a main loop/function, usually int main (). Is there a similar function that I can use in android Java development?

Schmitt answered 8/7, 2009 at 17:59 Comment(0)
C
13

As far as an Android program is concerned there is no main().
There is a UI loop that the OS runs that makes calls to methods you define or override in your program. These methods are likely called from/defined in onCreate(), onStart(), onResume(), onReStart(), onPause(), onStop(), or onDestroy(). All these methods may be overriden in your program.

The fundamental issue is that the OS is designed to run in a resource constrained environment. Your program needs to be prepared to be halted and even completely stopped whenever the OS needs more memory (this is a multitasking OS). In order to handle that your program needs to have some of all of the functions listed above.

The Activity lifecycle describes this best (your program is one or more Activities, think of an Activity as a screen).

Bottom line: Your program 'starts' at onCreate() through onResume() but the OS is running the loop. Your program provides callbacks to the OS to handle whatever the OS sends to it. If you put a long loop at any point in your program it will appear to freeze because the OS (specifically the UI thread) is unable to get a slice of time. Use a thread for long loops.

Conium answered 10/7, 2009 at 3:18 Comment(0)
F
6

In Android environment, there is no main(). The OS relies on the manifest file to find out the entry point, an activity in most case, into your application.

You should read http://developer.android.com/guide/topics/fundamentals.html for more detail.

Franke answered 8/7, 2009 at 20:34 Comment(0)
W
3

According to: http://developer.android.com/guide/tutorials/hello-world.html

The application class must support a method for each activity that the Application supports. In the general case, the onCreate is probably equivalent to the main/top function for your needs.

Wend answered 8/7, 2009 at 18:3 Comment(0)
G
1

Maybe it's possible by creating a timer and execute custom functions at every tick, reset the timer when it's at a specific time

Geoffrey answered 5/2, 2013 at 21:22 Comment(1)
This really can't be considered main function in that way.Excellent
M
0

The above answers provide a "why" as to there's no "main loop" on Android (which is important to understand). I'll offer a solution to the implied question, instead, as many visitors here will be looking for exactly that.

I believe the appropriate thing to do, here, would be to create an AsyncTask which operates as your "main loop". Or better yet, design your main loop to run as a java.util.concurrent future, which can be started and ended during lifecycle events (like rotation!), using signaling (keep your data separate). The AsyncTask API is deprecated, because it was complex, and handling it properly amounted to writing code that would, effectively, operate as an AsyncTask which cleaned up when the next problematic lifecycle event transpired.

Keep in mind that this will be a separate thread from the UI, and, as such, will be required to respond in short order to UI thread events, like "onPause" and "onDestroy". If your app does not respond within a certain period of time (~5 secs, iirc) to these events, or user input events, it will be killed by the OS. It's really prudent, for a real-time app, to be able to fully respond to these events in under 1 sec, even on the lowest-end device. You can use synchronization primitives to notify other threads that their response is pending, and they can use them to signal when they are finished (or simply end, in the case of a future).

Marsipobranch answered 26/7, 2022 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.