Activity学习篇
一个application由多个“松散”的activity组成。每个application都有其主入口,一个activity一旦停止,就进入一个堆栈里。当界面停止时,要求手动释放任何大的对象,比如网络或数据库,当界面进入resume时,你可以重新请求资源。
一个标准的activity应该要如下:
public class TabHost extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host_fourth_view);
}
}
继承Activity,两个最重要的回调函数是onCreate(),OnStop()。调用 setContentView() 定义用户界面。
任何一个界面的使用都得在AndroidManifest.xml里做出声明。在声明里用<intent-filter>来过滤,告诉其他程序可以怎么样来激活这个界面。比如
<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>
The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
If you intend for your application to be self-contained and not allow other applications to activate its activities, then you don't need any other intent filters.
启动一个界面
Intent intent= new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
上面代码是用来告诉启动系统启动发邮件的命令。在putExtra里放入邮件的地址。
启动一个有结果返回的界面
private void pickContact() { // Create an intent to "pick" a contact, as defined by the content provider URI Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // If the request went well (OK) and the request was PICK_CONTACT_REQUEST if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { // Perform a query to the contact's content provider for the contact's name Cursor cursor = getContentResolver().query(data.getData(), new String[] {Contacts.DISPLAY_NAME}, null, null, null); if (cursor.moveToFirst()) { // True if the cursor is not empty int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); String name = cursor.getString(columnIndex); // Do something with the selected contact's name... } } }
一个activity的生命周期
public class ExampleActivity extends Activity { @Override public voidonCreate
(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created. } @Override protected voidonStart()
{ super.onStart(); // The activity is about to become visible. } @Override protected voidonResume()
{ super.onResume(); // The activity has become visible (it is now "resumed"). } @Override protected voidonPause()
{ super.onPause(); // Another activity is taking focus (this activity is about to be "paused"). } @Override protected voidonStop()
{ super.onStop(); // The activity is no longer visible (it is now "stopped") } @Override protected voidonDestroy()
{ super.onDestroy(); // The activity is about to be destroyed. } }
第一次启动一个界面会经历OnCreate,OnStart,OnResume,按顺序执行。OnCreate是第一次创建界面时会调用。OnStart是指界面在被用户看见前被调用。OnResume代表界面获得用户焦点,此时用户可以看见界面并且焦点在界面上。在我认为中,onPasue和OnStop的区别就在于OnPause单纯指用户焦点不在此界面上,OnStop是指用户不仅焦点不在此界面上,连这个界面都看不到了。所以当弹出对话框,popWindow时activity是onPause.