How can Android source code not have a main method

2019-01-07 18:36发布

问题:

I've seen this in a few tutorials now... but how in the world can Android source code not have a main method and still run.

For example (from http://developer.android.com/guide/tutorials/hello-world.html):

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

That runs but there is no main!!!

I've also thought that using things like onCreate (or formLoad, etc.) was bad becuase a constructor should do that work and such built-in methods can be smelly sometimes. But onCreate is an entry point? Even without a main?

What if there is more than one activity... is there a hierarchy to these built in event handlers? OnCreate trumps everything else? Otherwise, how would the app know what to run or where to enter the program?

Thanks!

回答1:

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.



回答2:

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.



回答3:

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



回答4:

Actually, this type of pattern is not peculiar of Android, but happens whenever you have some framework in the middle. Some basic examples are java Applets and Servlets. Some of the answers already provide give the correct response, but I will try to elaborate a bit.

When you launch a Java app, you start a JVM and then you need to load something into it: so you need a static method (the main) because there are no objects (yet) living in the JVM that you can refer to.

If you have some sort of framework in the middle, it is the framework that will start the JVM and will start populating it with its own service objects: writing your code then means writing your own objects (which will be subclasses of given "template"). Your objects can then be injected (loaded) by the framework. The framework service objects manage the lifecycle of the injected objects by calling the lifecycle methods defined in the "template" superclass.

So for instance when you provide an applet to a browser, you do not launch a static main method: you rather only provide a subclass of java.applet.Applet that implements some instance methods which act as callback to manage the lifecycle (init, paint, stop...). It is the browser that will launch the JVM, instantiate what's needed for the launching an applet, load your applet and call it.

Similarly, with servlets you subclass the javax.servlet.http.HttpServlet class and implement some instance (non static) methods (doGet, doPost...). The Web container (e.g. Tomcat) will be in charge to launch the JVM, instantiate what's needed for launching a servlet, load your servlet and call it.

The pattern in Android is pretty much the same: what do you do is to create a subclass of android.app.Activity. When you launch an app, the system looks in the manifest to find out which activity should be started, then the "framework" loads it and calls its instance methods (onCreate, onPause, onResume...).



回答5:

In Java programs we need a main() method, because while executing the byte code the JVM will search for the main() method in the class and start executing there.

In Android, the Dalvik Virtual Machine is designed to find a class which is a subclass of Activity and which is set to start the execution of the application from its onCreate() method, so there is no need of a main() method.

The order in which Dalvik Virtual Machine calls methods is based on order of priorities called android life cycle for more information on android life cycle check the link below Android Life Cycle: https://developer.android.com/guide/components/activities/activity-lifecycle.html



回答6:

While there is no specific main entry point, intent filters describe which activity is started when the application is launched. They are controlled in AndroidManifest.xml as described here:

http://developer.android.com/guide/topics/intents/intents-filters.html

where a note pad application example is described:

This filter declares the main entry point into the Note Pad application. The standard MAIN action is an entry point that does not require any other information in the Intent (no data specification, for example), and the LAUNCHER category says that this entry point should be listed in the application launcher.



回答7:

An android programmer should learn this like the back of their hands it simply explains everything and would help in the future when creating activities. http://developer.android.com/reference/android/app/Activity.html



回答8:

There is a main of sorts, it just happens to be out of your hands. After all, there's nothing special about a main function in any language. It's just the entry point where your code starts executing. The Android operating system expects applications to have a certain structure and it calls your code based on the conventions you follow.



回答9:

I found this particularly useful...

http://developer.android.com/guide/topics/fundamentals.html#appcomp



回答10:

Applets don't have main() methods either. It just depends on how your code is packaged.



回答11:

The Android UI frame encapsulate some Java common details, you can study the source code of the android UI framework



回答12:

I think that Jonathon's answer is going in the right direction. He says the OS expects a certain structure. There's a name for that structure which is a "state machine". In this case Android calls it the "activity lifecycle". Rob gives a link to the documentation which contains an important diagram of that state machine though the text is a bit dry. A quick search also found me the following link that explains it fairly clearly: http://www.android-app-market.com/android-activity-lifecycle.html



回答13:

In Java, there is a main even if it isn't listed as main(). The page you get after the icon click, whatever its name, is the main().