Where is main() in Android?

JavaAndroid

Java Problem Overview


I am new to Android, and was studying the framework and it compelled me to ask this question. Since we are extending Activity in Android, there has to be main somewhere in Activity or there is an hidden class that Activity implements that contains main.

I looked everywhere but couldn't find it. I would appreciate if any one could give a clear idea on it.

Java Solutions


Solution 1 - Java

In core 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 the case of Android, the Dalvik Virtual Machine (After android 5.0 DVM is replaced by Android Runtime) is designed to find a class which is a subclass of Activity and which is set as a LAUNCHER to start the execution of the application from its onCreate() method, so there is no need of a main() method.

For more information see the life cycle of Activity.

Solution 2 - Java

Actually, the main() method is the Android framework class android.app.ActivityThread. This method creates the Main (UI) Thread for an OS process, sets up the Looper on it and starts the event loop.

The Android framework is responsible for creating and destroying OS processes, launching applications, starting activites, services and other components. The ActivityManager is part of the Android framework and it is responsible for coordinating and managing different components.

The architecture of Android is a bit different than you may be used to from stand-alone Java applications. The biggest difference is that all of your components (Activity, Service, BroadcastReceiver, etc.) do not necessarily run in the same OS process or in the same virtual machine (VM). It is possible to have components from a single application running in different OS processes and it is also possible to have components from different applications running in the same OS process. In traditional Java, the main() method is the method that is called by virtual machine after the OS process has been created and the virtual machine has completed its initialization.

Solution 3 - Java

Android uses the java language, but executes using a modified runtime model. As others have said, there is a manifest included in each package. The launchpoint is specified in this manifest. Go to the android site and do the basic tutorials. This will get you up and running with an understanding of create/deploy/run process and the basic app life cycle.

Solution 4 - Java

Read this blog entry to understand how an Android application starts:

> During startup of the Android system the Linux kernel first calls the > process "init". init reads the files "/init.rc" and "init.device.rc". > "init.device.rc" is device specific, on the virtual device this file > is called "init.goldfish.rc". > > init.rc starts the process "Zygote" via the program "/system/bin/app_process". Zygote loads the core Java classes and > performs initial processing of them. These classes can be reused by > Android application and therefore this step makes them faster to > start. Once the initial work of Zygote is done, the process listens to > a socket and waits for requests.

If you look in the ZygoteInit class, you'll find the main method.

As others have mentioned, this main() method is involved in setting up the Android app environment. As far as a developer is concerned, the starting point is the onCreate() method of the Launcher activity.

Solution 5 - Java

In Android, the OS uses Dalvik virtual machine. The main entry point to the execution of the application is encapsulated within the framework. You might want to take a look at "What is Android?"

In fact, each Activity in Android can be thought to be a single Application on its own with a lifecycle of its own.

Solution 6 - Java

onCreate() Method....See lifecycle of android class (Activity).....

http://developer.android.com/reference/android/app/Activity.html

Solution 7 - Java

You tell it which one to run on startup in the manifest file. There isn't a main() function 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() method, 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 this

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.

Solution 8 - Java

When you start a new process to run a App/Service, finally in ActivityManagerService.java, there is:

final String entryPoint = "android.app.ActivityThread";

return startProcessLocked(hostingType, hostingNameStr, entryPoint, app, uid, gids,
                runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith,
                startTime);

Which finally calls Process.start. This is the same with the standard java, you pass in a class then the VM create a new process and execute main() of ActivityThread.java:

public static void main(String[] args) {
         ......
    Looper.loop();

    throw new RuntimeException("Main thread loop unexpectedly exited");
}

The main function will trigger some action that send IPC message to notify Activity Manager that the process has started successfully, then after notifying the process that initiate the start of the new process of this event, Activity Manager will notify the new process to do the real activity startup process, which create the Activity class according to the manifest then call OnCreate etc.

There is a few answer here that is totally wrong which get a lot of votes, hope a moderate etc can check this.

Solution 9 - Java

here is the main method

Look at the stack frame it starts from the main().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBasimalla SebastinView Question on Stackoverflow
Solution 1 - JavaChandra SekharView Answer on Stackoverflow
Solution 2 - JavaDavid WasserView Answer on Stackoverflow
Solution 3 - JavaBobFView Answer on Stackoverflow
Solution 4 - JavaW.K.SView Answer on Stackoverflow
Solution 5 - JavamaurisView Answer on Stackoverflow
Solution 6 - JavaSamir MangroliyaView Answer on Stackoverflow
Solution 7 - JavaFranklineView Answer on Stackoverflow
Solution 8 - Javajw_View Answer on Stackoverflow
Solution 9 - JavaNeeraj SharmaView Answer on Stackoverflow