54

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Basimalla Sebastin
  • 1,159
  • 3
  • 9
  • 15

9 Answers9

73

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.

Bala
  • 137
  • 2
  • 10
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 6
    Your explanation describes how Android decides which `Activity` to launch when the user opens an App, but this has nothing to do with the underlying application framework launch process (which is actually quite complicated). The `main()` method is in the Android framework class `android.app.ActivityThread`. This method creates the Main (UI) `Thread`, sets up a `Looper` on it and starts the event loop. There are other parts of the Android framework that deal with starting/stopping OS processes, launching applications and activities and services and other components, etc. – David Wasser Aug 15 '17 at 13:28
  • @DavidWasser, you are right, but I tried to answer it as simple as possible. OP fails to understand about main() and to answer if we start explaining about ActivityThread, Looper, etc doesn't makes any sense. – Chandra Sekhar Aug 16 '17 at 17:24
  • 3
    Actually, `onCreate()` of the `Application` class (or a subclass of `Application`) is called before `onCreate()` of an `Activity`. Also, there are Android applications that start with a `Service` or a `BroadcastReceiver` and not with an `Activity`. – David Wasser Apr 17 '18 at 09:03
27

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    +1 but to be very precise, Main and UI threads might not be the same on android. See https://stackoverflow.com/questions/40784584/difference-between-the-main-thread-and-ui-thread – YTerle Apr 17 '18 at 08:03
  • 3
    @YTerle thanks for the link. For all intents and purposes (ie: normal applications) Main and UI threads are the same. If you read the linked answer there is only one exception and that is part of the Android framework itself. – David Wasser Apr 17 '18 at 08:59
18

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.

BobF
  • 314
  • 1
  • 3
14

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.

M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • 3
    Actually, `onCreate()` of the `Application` class (or a subclass of `Application`) is called before `onCreate()` of an `Activity`. Also, there are Android applications that start with a `Service` or a `BroadcastReceiver` and not with an `Activity`. – David Wasser Apr 17 '18 at 09:03
  • The link to `ZygoteInit` is (A) "dead" and (B) misspelt. – naXa stands with Ukraine Oct 25 '18 at 20:01
3

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.

mauris
  • 42,982
  • 15
  • 99
  • 131
2
onCreate() Method....See lifecycle of android class (Activity).....

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

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

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.

Frankline
  • 40,277
  • 8
  • 44
  • 75
0

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.

jw_
  • 1,663
  • 18
  • 32
0

here is the main method

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

Neeraj Sharma
  • 371
  • 3
  • 4