简体   繁体   English

活动和布局之间的联系? 如何更改布局? 如何开始和破坏活动?

[英]Connection between activity and layout? How to change layout? How to start and destroy an activity?

I'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android: 我是Android和Eclipse的初学者,我有几个问题可以帮助我理解Android的理念:

  1. The activity does all the work "behind the scenes". 该活动在“幕后”完成所有工作。 Activities are connected to a layout in the XML file. 活动连接到XML文件中的布局。 When the activity is started, if declared in setContentView method, the connected layout will be shown. 当活动开始时,如果在setContentView方法中声明,则将显示连接的布局。 Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout. 活动可以是独立的,没有任何布局,它可以由另一个活动调用,并且可以在不显示任何布局的情况下完成所有工作。 Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements. 活动类似于一个php文件,该文件由HTML中的我的提交按钮调用,布局是显示元素的.HTML。

Am I right with this one? 我对吗?

  1. For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml . 例如,如果要更改应用程序的布局,请单击Layout1.xml按钮时显示Layout2.xml Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml ? 然后,我必须销毁与Layout1.xml连接的活动,并启动与Layout2.xml连接的活动? Is this correct? 这个对吗? Is there any better way to do this? 有什么更好的方法吗?

  2. How can I (by which method) destroy/stop a certain activity? 如何(通过哪种方法)破坏/停止某种活动?

Thank you in an advance. 预先感谢您。

The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html 最好的选择是阅读有关活动的Android文档, 网址http://developer.android.com/reference/android/app/Activity.html

I will answer your specific questions here though 我会在这里回答您的具体问题

  1. An Activity is a window that the user can see (or a hidden window if there is no layout defined). 活动是用户可以看到的窗口(如果未定义布局,则为隐藏窗口)。 It deals with the logic of a part of the app that the user can see and interact with. 它处理了用户可以查看和交互的应用程序部分的逻辑。 If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout). 如果我们采用MVC模型(模型视图控制器),则活动就是控制器,就它控制模型中的哪些数据显示在视图(xml布局)上而言。

  2. If you want to show a new window/screen/activity you do not need to destroy the current one. 如果要显示新的窗口/屏幕/活动,则不需要销毁当前窗口/屏幕/活动。 You can open a new activity whilst keeping the old one in the background (in the back stack). 您可以打开一个新活动,同时将旧活动保留在后台(在后堆栈中)。 With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. 通过使用片段,您可以在一个活动中包含多个片段,因此您可以更改单个活动中的片段,而不是更改活动。 For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html . 有关片段的更多信息,请访问http://developer.android.com/reference/android/app/Fragment.html

  3. This point relies heavily on the activity lifecycle. 这一点严重依赖于活动生命周期。 When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background). 当活动被销毁时,表示活动已完成,这可以通过以下方式完成:用户可以在活动上按下返回按钮,活动本身调用finish()或Android操作系统销毁活动,因为其他地方需要内存(当应用在后台运行时可能会发生这种情况)。

When we say an activity is stopped, it means that the activity is no longer visible to the user. 当我们说某个活动已停止时,这意味着该活动不再对用户可见。 This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background. 可能是该活动位于后台堆栈中(另一个活动位于其前面),或者该应用程序已置于后台。

This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge. 这是对您问题的简短回答,但我强烈建议您阅读Android文档以获取更多知识。

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

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