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

前端 未结 13 2458
萌比男神i
萌比男神i 2020-11-30 07:40

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.andro

相关标签:
13条回答
  • 2020-11-30 08:13

    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.

    0 讨论(0)
  • 2020-11-30 08:16

    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

    0 讨论(0)
  • 2020-11-30 08:16

    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

    0 讨论(0)
  • 2020-11-30 08:21

    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.

    0 讨论(0)
  • 2020-11-30 08:23

    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.

    0 讨论(0)
  • 2020-11-30 08:28

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

    0 讨论(0)
提交回复
热议问题