前一篇文章因为是晚上写的,最后写的不完善
今天把自己学习Activity的过程和经验记录一下,希望能帮助到有心看这篇文章的朋友。本人水平低陋,还望指教
首先看代码:
package com.zph; import android.app.Activity; import android.os.Bundle; public class FirstActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
第二行:setContentView(R.layout.main);
设置布局,到哪去找呢?打开Eclipse左边的视图:
在res目录下的layout文件夹下有一个main.xml
代开R.java
在R.java里有一个值public static final int main=0x7f030000 对应main.xml文件,在 setContentView(R.layout.main)查找到了main.xml,然后读取其中的布局配置
这个程序就不演示了
言归正传,什么是Activity?
这里我没翻译,Google翻译的是活动,但感觉不太好,干脆就不翻译了,我也没听说过谁翻译过他
打开android的开发文档,两种方法:1,android的安装路径下有个doc文件夹,打开找到index.html
2,http://developer.android.com/index.html
先大致浏览一下:
我们主要用到的是 Dev Guide 和 Reference
Reference是API
Dev Guide是开发向导,打开
找到Activities,第一句:
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
简单的说就是一个Activity就是一个屏幕,用户可以点击、触摸。一个应用程序可能不止一个Activity。Activity的尺寸也不一定就是和手机屏幕的尺寸一样大,有可能要小。很简单的一个例子:我们在用手机的时候突然来了个电话,显示来电的那个Activity就没有全部覆盖屏幕。
接着往下看:
Creating an Activity To create an activity, you must create a subclass of Activity (or an existing subclass of it). In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are: onCreate() You must implement this method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface. onPause() The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). There are several other lifecycle callback methods that you should use in order to provide a fluid user experience between activities and handle unexpected interuptions that cause your activity to be stopped and even destroyed. All of the lifecycle callback methods are discussed later, in the section about Managing the Activity Lifecycle.
Implementing a user interface The user interface for an activity is provided by a hierarchy of views—objects derived from the View class. Each view controls a particular rectangular space within the activity's window and can respond to user interaction. For example, a view might be a button that initiates an action when the user touches it. Android provides a number of ready-made views that you can use to design and organize your layout. "Widgets" are views that provide a visual (and interactive) elements for the screen, such as a button, text field, checkbox, or just an image. "Layouts" are views derived from ViewGroup that provide a unique layout model for its child views, such as a linear layout, a grid layout, or relative layout. You can also subclass the View and ViewGroup classes (or existing subclasses) to create your own widgets and layouts and apply them to your activity layout. The most common way to define a layout using views is with an XML layout file saved in your application resources. This way, you can maintain the design of your user interface separately from the source code that defines the activity's behavior. You can set the layout as the UI for your activity with setContentView(), passing the resource ID for the layout. However, you can also create new Views in your activity code and build a view hierarchy by inserting new Views into a ViewGroup, then use that layout by passing the root ViewGroup to setContentView().
<?xml version="1.0" encoding="utf-8"?>
后面的直接从文档里拿过来:
<manifest ... > <application ... > <activity android:name=".ExampleActivity" /> ... </application ... > ... </manifest >
<intent-filter>加到activity标签里就可以了:
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Intent intent = new Intent(this, SignInActivity.class); startActivity(intent);
Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_EMAIL, recipientArray); startActivity(intent);
很明显,隐式意图只需要告诉Android我要干嘛就可以了,在这里是告诉android我要发Email,当然还有很多其他的Intent,像打电话、发短信、web搜索等等.
简单写了一下intent之后我都要晕了,都要忘了今天是学习Activity的。还是看上面的代码的,我们创建了一个Intent,
然后要执行,调用的方法是:startActivity(intent),看名字就知道,开始一个Activity,说明我们的Activity还可以这样创建。
要结束一个Activity,方法是:finish(),但文档里明确说了,除非有必要,不要自己去调用finish()方法,这个必要是什么呢:
Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity.
Activity Lifecycle
看到这些先不要急,喝杯水,这些内容很重要。
一个Activity开始的时候首先调用onCreate()方法,在这个方法里我们需要把程序的准备工作都做好,比如说创建视图,绑定数据等等,这个方法传入一个Bundle 对象,这个对象包含有Activity以前的状态(前提是这个状态被捕获了)。
onCreate()后开始onStart(),文档里对onStart()方法没有过多描述:
Called just before the activity becomes visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
However, even if you do nothing and do not implement onSaveInstanceState(), some of the activity state is restored by the Activity class's default implementation of onSaveInstanceState(). Specifically, the default implementation calls onSaveInstanceState() for every View in the layout, which allows each view to provide information about itself that should be saved. Almost every widget in the Android framework implements this method as appropriate, such that any visible changes to the UI are automatically saved and restored when your activity is recreated. For example, the EditText widget saves any text entered by the user and the CheckBox widget saves whether it's checked or not. The only work required by you is to provide a unique ID (with the android:id attribute) for each widget you want to save its state. If a widget does not have an ID, then it cannot save its state.
右图把Activity状态恢复的流程写的很清楚,前提是有一个onSaveInstanceState()方法,有些时候需要自己覆写onSaveInstanceState()方法,个人觉得不管父类有没有覆写,子类都要覆写一下。还有就是文档中十分强调了:持久化的数据不要放在onSaveInstanceState()方法里,这个方法适合放UI的状态信息,持久化的数据保存工作应该放在onPause()方法中:
Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.