How can Android source code not have a main method

2019-01-07 18:12发布

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!

13条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-07 18:57

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

查看更多
再贱就再见
3楼-- · 2019-01-07 18:59

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-07 18:59

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

查看更多
来,给爷笑一个
5楼-- · 2019-01-07 18:59

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

查看更多
姐就是有狂的资本
6楼-- · 2019-01-07 19:02

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.

查看更多
再贱就再见
7楼-- · 2019-01-07 19:05

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.

查看更多
登录 后发表回答