How is an Android activity instantiated (using reflection)? How is an Android activity instantiated (using reflection)? android android

How is an Android activity instantiated (using reflection)?


When an app's launcher icon is clicked on homescreen, following event happens under the android system :

  • Homescreen/Launcher app sends an intent to start an activity using startActivity()(startActivity() is binder call to ActivityManager)
  • Activity Manager sends a process fork request using a socket to Zygote.
  • Zygote forks a new VM instance that loads ActivityThread(Activity thread manages the execution of the main thread in an application process, scheduling and executing activities, broadcasts, and other operations on it as the activity manager requests.).
  • ActivityThread has real main() for an app.
  • ActivityThread calls the app's onCreate().

Hence ActivityThread is responsible for instantiating Activity(inside performLaunchActivity method)

Explanation :

If you observe the stacktrace :

android.app.Instrumentation.newActivity(Instrumentation.java:1021)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

Code where new activity is instantiated :

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {    ... //More code    Activity activity = null;    try {        java.lang.ClassLoader cl = r.packageInfo.getClassLoader();        activity = mInstrumentation.newActivity(                cl, component.getClassName(), r.intent);        StrictMode.incrementExpectedActivityCount(activity.getClass());        r.intent.setExtrasClassLoader(cl);        r.intent.prepareToEnterProcess();        if (r.state != null) {            r.state.setClassLoader(cl);        }    } catch (Exception e) {        if (!mInstrumentation.onException(activity, e)) {            throw new RuntimeException(                "Unable to instantiate activity " + component                + ": " + e.toString(), e);        }    }    ... //More code    return activity;}

Instrumentation.java(class will be instantiated for you before any of the application code)

public Activity newActivity(ClassLoader cl, String className,        Intent intent)        throws InstantiationException, IllegalAccessException,        ClassNotFoundException {    return (Activity)cl.loadClass(className).newInstance();}


The simple way to check the path to the constructor method is to create a temporary project, override constructor in your Activity and place breakpoint there.

You should be able to walk through the all code and find what exactly you want.

enter image description here


As long as you are not in an interview for an Android system developer (kernel hacker, ...) the answer is simply: That is an implementation detail of the Android framework a normal Android developer should not need to care about because of the abstraction and layer principle and it can be looked up in the rare case you would really need to know it.