0

When creating a new app, we first define the MainActivity class and then fill its OnCreate , OnPause, OnResume methods etc.

Now, MainActivity is not a static class, and yet, without any instantiation, we are able to see code execution of the methods in it. How does this work?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • 2
    but you would have declared it in the manifest file. those are all lifecycle methods. you only override those methods – Raghunandan Jan 15 '14 at 10:40
  • don't be confused with the main method of java ... http://stackoverflow.com/questions/4221467/how-can-android-source-code-not-have-a-main-method-and-still-run – stinepike Jan 15 '14 at 10:41
  • It is android that takes care to instantiate the Activity for you. – Blackbelt Jan 15 '14 at 10:42
  • The Android Framework looks at your Manifest file, instantiates the necessary classes and calls the necessary startup methods. – TheLostMind Jan 15 '14 at 10:43
  • @Raghunandan: So you mean that what we write in the xml file AndroidMainfest.xml under the ** section qualifies as an instantiation? – SoulRayder Jan 15 '14 at 10:43
  • @Gautham yes you declare an activity in manfiest file. if android does nto find one you get `ActivityNotfoundException`. also you make an entry for services in manifest if you app has one. – Raghunandan Jan 15 '14 at 10:44

2 Answers2

5

the AndroidManifest file has this enty

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

that is how the MainActivity get started .. if you click on the icon from your launcher the MainActivity starts

you can have multiple activities specified but they dont have intent-filters on them. this intent filter says the main way the app starts from the launcher should start this activity

that is what intent-filters does

LeMoN.xaH
  • 571
  • 2
  • 9
  • your only partially right, you can have multiple intentfilters, but i guess only one should catch the launcher event – Daniel Bo Jan 15 '14 at 11:22
5

There is nothing wrong in asking basic questions... 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.

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

Marco
  • 423
  • 5
  • 7