This is a question out of curiosity and not an actual problem. A stand alone java program that's executable requires a public static void main(String[] args) method. But in android there is no such method. Only thing closest to a main method is the activity defined as category android:name="android.intent.category.LAUNCHER in the manifest. So my question is does android require a main method? If yes how is it generated, is it by parsing the xml file and creating references like R.java in gen folder similar to res folder?? If not how does the execution of my java files start.
Asked
Active
Viewed 961 times
0
-
1@rgamber that link doesnot answer my question – Illegal Argument Jun 20 '14 at 17:28
-
@DerGolem main method is singular entry point of a program so I dont think thats the case. – Illegal Argument Jun 20 '14 at 17:33
-
@DerGolem Every app has a main- an initial entry point where the program starts. Sometimes it just has a different name or is part of the runtime/framework. – Gabe Sechan Jun 20 '14 at 17:44
2 Answers
4
There is a main, but you didn't write it. The main is part of the Android framework. It will take the parameters and map them to an Activity or Service to run, then call the proper lifecycle functions. So you don't have to worry about it. Think of the onCreate of an Activity (or onStart of a service) replacing main.

Gabe Sechan
- 90,003
- 9
- 87
- 127
-
thats the confusing part *there is a main* if it is in the framework then its not mine. Also your comment above *Every app has a main* is that main coming from the dalvik virtual machine that a app runs on. Every app will run on its own vm so is that the case. I am trying to be an android trainer but I need to learn a lot myself – Illegal Argument Jun 20 '14 at 17:48
-
1Think about it this way- every app has one function that will be called first. We call that a "main function". In standard Java, that's a function called main. Same in standard C++. In C++ for windows, its a function called WinMain. Some old C++ frameworks had one called TMain. Here its a small piece of code built in already that decides what activity to launch, then calls the proper lifecycle functions. Basically, you shouldn't need to worry about it except from an academic standpoint- just teach the activity lifecycle. – Gabe Sechan Jun 20 '14 at 18:13
-
-
@juztcode No, and you wouldn't want to. The rest of the framework wouldn't be set up correctly. If you need to do something when the application starts, there's Application.onCreate (although it should be something quick as I believe it has watchdog timers)- . – Gabe Sechan Feb 01 '20 at 16:30
0
The Java Virtual Machine that you use to run standalone Java programs needs main() to begin execution. Android uses currently uses the Dalvik Virtual Machine which searches for an instance of an Activity child class. In Android 5 they're switching to ART (Android Runtime). As you mentioned, the manifest specifies the LAUNCHER whose value is this child class and execution starts in onCreate().

nocdib
- 1,286
- 10
- 7