0

我们在 Android 中编写不同的活动类,并在 AndroidManifest.xml 中声明这些活动

但是,我没有得到的是该 Activity 类的对象是何时创建的,或者该 Activity 类是如何加载到内存中的?

这种怀疑可能看起来很天真,但很困惑。

4

2 回答 2

0

An Activity is instantiated automatically by your Application when it receives an Intent that corresponds with one of the Activities you describe in your manifest. You don't need to worry about any constructor methods, or keeping a reference to your activity after its instantiated. That is done behind the scenes. Android handles the lifecycle, so you use the lifecycle callbacks to handle creation and cleanup of your own objects.

When someone clicks the icon for your app in the launcher, what actually happens is the launcher sends an Intent to your application to launch the activity associated with that Intent. If your application isn't open yet, Android will launch it so it can receive the Intent.

于 2013-10-25T15:50:15.420 回答
0

活动是用户可以做的一个单一的、有重点的事情。几乎所有活动都与用户交互,因此 Activity 类负责为您创建一个窗口,您可以在其中放置带有 setContentView(View) 的 UI。该活动包含应用程序的用户界面。有各种活动状态,例如 Running、Paused、Stopped 和 Killed。Activity 基类包含几个控制活动生命周期的事件。

现在,当调用Activities 的onDestroy 方法时,您的Activity 不一定会被垃圾回收。当系统内存不足时,您的应用程序所在的进程可能会被杀死,这意味着您的应用程序将消失;应用程序的 onTerminate 方法可能会或可能不会被调用。那时所有的活动、服务等也都被杀死了。Application 实例总是首先被实例化,一个 Activity 必须有一个关联的 Application,就像你在 AndroidManifest.xml 中定义它一样。

与往常一样,了解这一切的最佳资源是官方文档

于 2013-10-25T15:46:28.200 回答