简体   繁体   English

什么时候在Android中创建Activity子类的对象?

[英]When is the object of Activity sub class created in Android?

We write different activity classes in Android and declare those activities in AndroidManifest.xml 我们在Android中编写不同的活动类,并在AndroidManifest.xml中声明这些活动

However, what I am not getting is when is the object of that Activity class created or how that activity class is loaded into memory ? 但是,我没有得到的是何时创建该Activity类的对象或该Activity类如何加载到内存中?

The doubt might seem naive but am confused. 怀疑似乎很幼稚,但却很困惑。

An activity is a single, focused thing that the user can do. 活动是用户可以做的一件专注的事情。 Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). 几乎所有活动都与用户交互,因此Activity类负责为您创建一个窗口,您可以在其中放置带有setContentView(View)的UI。 The activity contains the user interface of your application. 该活动包含您的应用程序的用户界面。 There are various states of activity like Running, Paused, Stopped and Killed. 活动的状态多种多样,例如正在运行,已暂停,已停止和已终止。 The Activity base class contains several events that govern the life cycle of an activity. Activity基类包含一些控制活动生命周期的事件。

Now, when Activities's onDestroy method is called, your Activities aren't necessarily garbage collected. 现在,当调用Activity的onDestroy方法时,不一定要对您的Activity进行垃圾收集。 When the system gets low on memory the process that your Application lives in can be killed, meaning your Application will disappear; 当系统内存不足时,您的应用程序所驻留的进程可能会被杀死,这意味着您的应用程序将消失。 Application's onTerminate method may or may not be called. 应用程序的onTerminate方法可以调用,也可以不调用。 At that time all the Activities, Services, etc, are killed too. 那时所有的活动,服务等也都被杀死了。 The Application instance is always instantiated first, an Activity must have an associated Application, just like how you define it in the AndroidManifest.xml. 总是首先实例化Application实例,一个Activity必须具有一个关联的Application,就像您在AndroidManifest.xml中定义它的方式一样。

As always, the best resource for understanding all this is the official documentation . 与往常一样,了解所有这些的最佳资源是官方文档

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. Android处理生命周期,因此您可以使用生命周期回调来处理自己对象的创建和清除。

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. 当某人在启动器中单击您的应用程序的图标时,实际发生的是启动器向您的应用程序发送一个Intent以启动与该Intent相关联的活动。 If your application isn't open yet, Android will launch it so it can receive the Intent. 如果您的应用程序尚未打开,Android将启动它,以便可以接收到Intent。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM