tags:

views:

1010

answers:

3

I'm pretty new to java in general, but I have a bit of experience in C and C++. 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?

+2  A: 

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.

drudru
+5  A: 

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.

weilin8
+4  A: 

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):

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

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.

Will