Android有三個基礎組件Activity,Service和BroadcastReceiver,他們都是依賴Intent來啟動。本文介紹的是Activity的生命周期以及針對Activity的Intent使用。
之前的例子一直都是使用Activity,在一個Layout XML與一個Activity捆綁的情況下可以視為一個Form,多個Layout XML與一個Activity捆綁的話那就是個Application本身了。Intent可以分為顯式Intent和隱式Intent:顯式Intent用於啟動明確的目標組件(前面所說的三大組件),同一個Application內的多個Activity調用也是顯式Intent;隱式Intent就是調用沒有明確的目標組件,可以是系統也可以是第三方程序。隱式Intent一般用於調用系統組件功能,相關例程都是網絡上很容易找到的(調用某些系統組件的時候要申請權限)。
Acitivity的運行狀況分為:onCreate、onDestroy、onStart、onStop、onRestart、onResume、onPause,onCreate對應onDestroy,onStart對應onStop,onResume對應onPause。
先貼出本文運行截圖:
這個是從Acitivity1轉到Activity2的時候,Acitivity1的狀態變化,使用了finish()會觸發onDestroy()。
這個是從Activity2轉到Activity1的時候,Acitivity2的狀態變化。從兩次Activity的啟動可以看出,流程是onCreate()->onStart()->onResume()三個方法,銷毀是onPause()->onStop()->onDestroy()。另外,要往工程添加第二個Activity,需要到AndroidManifest.xml->Application那裡添加Activity2。
main1.xml的源碼:
XML/HTML代碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/main1.Button01"
- android:text="跳轉到Activity2"></Button>
- <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/main1.Button02"
- android:text="跳轉到外部Activity"></Button>
- </LinearLayout>
main2.xml的源碼:
XML/HTML代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout android:id="@+id/LinearLayout01"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/main2.Button01"
- android:text="返回Activity1"></Button>
- </LinearLayout>
Activity1的源碼:
Java代碼
- package com.testActivityIntent;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class testActivityIntent extends Activity {
- /** Called when the activity is first created. */
- Button btnToInternalActivity;
- Button btnToExternalActivity;
- EditText tbBundle;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.e("Activity1", "onCreate");//顯示當前狀態,onCreate與onDestroy對應
- setContentView(R.layout.main1);
-
- btnToInternalActivity=(Button)this.findViewById(R.id.main1_Button01);
- btnToExternalActivity=(Button)this.findViewById(R.id.main1_Button02);
- btnToInternalActivity.setOnClickListener(new ClickEvent());
- btnToExternalActivity.setOnClickListener(new ClickEvent());
- tbBundle=(EditText)this.findViewById(R.id.EditText01);
- }
- public void onDestroy()
- {
- super.onDestroy();
- Log.e("Activity1", "onDestroy");//顯示當前狀態,onCreate與onDestroy對應
- }
- @Override
- public void onStart()
- {
- super.onStart();
- Log.e("Activity1", "onStart");//顯示當前狀態,onStart與onStop對應
- }
- @Override
- public void onStop()
- {
- super.onStop();
- Log.e("Activity1", "onStop");//顯示當前狀態,onStart與onStop對應
- }
- @Override
- public void onRestart()
- {
- super.onRestart();
- Log.e("Activity1", "onRestart");
- }
- @Override
- public void onResume()
- {
- super.onResume();
- Log.e("Activity1", "onResume");//顯示當前狀態,onPause與onResume對應
- SharedPreferences prefs = getPreferences(0); //SharedPreferences 用於存儲數據
- String restoredText = prefs.getString("editText01", null);
- if (restoredText != null) {
- this.tbBundle.setText(restoredText);
- }
- }
- @Override
- public void onPause()
- {
- super.onResume();
- Log.e("Activity1", "onPause");//顯示當前狀態,onPause與onResume對應
- //保存文本框的內容,使得重回本Acitivity的時候可以恢復
- SharedPreferences.Editor editor = getPreferences(0).edit();//SharedPreferences 用於存儲數據
- editor.putString("editText01", this.tbBundle.getText().toString());
- editor.commit();
- }
-
- class ClickEvent implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- if(v==btnToInternalActivity)
- {
- Intent intent = new Intent();
- intent.setClass(testActivityIntent.this,Activity2.class);
-
- //new一個Bundle對象,並將要傳遞的數據傳入
- Bundle bundle = new Bundle();
- bundle.putString("Text",tbBundle.getText().toString());
-
- //將Bundle對象assign給Intent
- intent.putExtras(bundle);
-
- //調用Activity2
- startActivity(intent);
-
- testActivityIntent.this.finish();//會觸發onDestroy();
- }
- else if(v==btnToExternalActivity)
- {
- //有些外部調用需要開啟權限
- Uri uri = Uri.parse("http://google.com");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- }
-
- }
-
- }
-
- }
Activity2的源碼:
Java代碼
- package com.testActivityIntent;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- public class Activity2 extends Activity {
- Button btnBackMain1;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- Log.e("Activity2", "onCreate");//顯示當前狀態,onCreate與onDestroy對應
-
- //加載activity2.xml
- setContentView(R.layout.main2);
-
- //得Intent中的Bundle對象
- Bundle bunde = this.getIntent().getExtras();
- //取得Bundle對象中的數據
- Log.i("In_Text", bunde.getString("Text"));
- btnBackMain1=(Button)this.findViewById(R.id.main2_Button01);
- btnBackMain1.setOnClickListener(new ClickEvent());
- }
-
- public void onDestroy()
- {
- super.onDestroy();
- Log.e("Activity2", "onDestroy");//顯示當前狀態,onCreate與onDestroy對應
- }
- @Override
- public void onStart()
- {
- super.onStart();
- Log.e("Activity2", "onStart");//顯示當前狀態,onStart與onStop對應
- }
- @Override
- public void onStop()
- {
- super.onStop();
- Log.e("Activity2", "onStop");//顯示當前狀態,onStart與onStop對應
- }
- @Override
- public void onRestart()
- {
- super.onRestart();
- Log.e("Activity2", "onRestart");
- }
- @Override
- public void onResume()
- {
- super.onResume();
- Log.e("Activity2", "onResume");//顯示當前狀態,onPause與onResume對應
- }
- @Override
- public void onPause()
- {
- super.onResume();
- Log.e("Activity2", "onPause");//顯示當前狀態,onPause與onResume對應
- }
-
- class ClickEvent implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- if(v==btnBackMain1)
- {
-
- Intent intent = new Intent();
- intent.setClass(Activity2.this,testActivityIntent.class);
-
- //調用Activity1
- startActivity(intent);
-
- Activity2.this.finish();//會觸發onDestroy();
- }
-
- }
-
- }
- }