如何实例化Android活动(使用反射)? [英] How is an Android activity instantiated (using reflection)?

查看:99
本文介绍了如何实例化Android活动(使用反射)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在Android采访中问过这个问题。我回答了平时,你知道,意图+ startActivity等等。然后采访者更有针对性地问道,

Got asked this today in an Android interview. I answered the usual, you know, intent + startActivity, etc. Interviewer then asked more pointedly,


是的,但实际上它在哪里实例化?你不会在任何地方调用新的
活动。

"Yes, but where is it actually instantiated? You don't call new Activity anywhere".

现在哪个我想一想,我真的不知道。他暗示它使用了Java反射,但我没有很多经验,我只是用它来访问Android SDK中的一些变量。

Which now that I think about it, I don't really know. He hinted that it used Java reflection, but I dont have a lot of experience with that, and I've only used it to access some variables in the Android SDK.

有人可以解释如何使用反射实例化活动,为什么?奖励点可以深入了解采访者在了解这一点时所看到的价值。

Can someone explain how Activities are instantiated using reflection, and why? Bonus points for insight into what value the interviewer saw in knowing this.

推荐答案

当在主屏幕上点击应用程序的启动器图标时,事件发生在android系统下:

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



  • Homescreen / Launcher应用程序使用startActivity发送启动活动的意图( )(startActivity()是对ActivityManager的绑定器调用)

  • 活动管理器使用套接字向Zygote发送进程fork请求。

  • Zygote for a new加载ActivityThread的VM实例(活动线程管理应用程序进程中主线程的执行,在活动管理器请求时调度和执行活动,广播以及其他操作。)。

  • ActivityThread具有应用程序的真实main()。

  • ActivityThread调用应用程序的onCreate()。

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

因此,ActivityThread是resp用于实例化Activity(在performLaunchActivity方法内)

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

说明:

如果你观察到栈跟踪:

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

实例化新活动的代码:

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(将在任何应用程序代码之前为您实例化类)

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();
}

这篇关于如何实例化Android活动(使用反射)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆