How can Android source code not have a main method and still run?

Each application will be having it's own Virtual Machine. To run an app, within it's space (VM), must have a main method.

Activities are not the actual classes to be invoked for start of application. There is a class called Application, which will be the root class for an application to be launched.

If there is no main method, how can a VM recognize how to start an app?

Framework has classes called Process, VMRuntime which are responsible for starting an application. Which indeed deal with main method.

For better understanding, study the Zygote service of Android. deals with Applicationmanager Service, ActivityStack Activity Threadds etc.


That runs but there is no main!!!

Of course. Many things that you might think of as a Java "application" do not have their own main() method. For example, IIRC, servlets, WARs, and the like do not have main() methods -- the main() method, if there is one, is in the container.

But onCreate is an entry point?

onCreate() is a method.

What if there is more than one activity... is there a hierarchy to these built in event handlers?

Not really.

OnCreate trumps everything else?

Not really.

Otherwise, how would the app know what to run or where to enter the program?

An app does not "know what to run or where to enter the program".

An Android application is a basket of components. Some components may be tied to icons in a home screen launcher. Some components may be tied to scheduled timers, like cron jobs or Windows scheduled tasks. Some components may be tied to system events, such as when the device is placed into or removed from a car dock. Those components will be automatically created and used when appropriate (e.g., when a user taps the icon in the home screen launcher). Yet other components are only created and used when your code specifically asks for them.

Thinking of an Android application as if it were a monolithic console-mode Java program will cause you no end of trouble.


You tell it which one to run on startup in the manifest file. There isn't a main() because there doesn't have to be, main may be a convention used for "regular" java apps, but it isn't for things like browser applets. The system creates the activity object and calls methods within it, which may or may not be called main. In this case, it's not.

onCreate is different from a main, and from a constructor, in that it can be called twice on a single activity, such as if the process is killed and the user navigates back to the activity. See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle