4.Android Activity+Intent

Android有三个常用基础组件Activity、Service和BroadcastReceiver,他们都是依赖Intent来启动。本文介绍的是Activity的生命周期以及针对Activity的Intent使用。

之前的例子一直都是使用Activity,在一个Layout XML与一个Activity捆绑的情况下可以视为一个Form,多个Layout XML与一个Android Activity捆绑的话那就是个Application本身了。

Android Intent可以分为显式Intent和隐式Intent:显式Intent用于启动明确的目标组件(前面所说的三大组件),同一个Application内的多个Activity调用也是显式Intent;隐式Intent就是调用没有明确的目标组件,可以是系统也可以是第三方程序。隐式Intent一般用于调用系统组件功能,相关例程都是网络上很容易找到的(调用某些系统组件的时候要申请权限)。

Acitivity的运行状况分为:onCreate、onDestroy、onStart、onStop、onRestart、onResume、onPause,onCreate对应onDestroy,onStart对应onStop,onResume对应onPause。

程序运行截图:

testActivityIntent

log

这个是从Acitivity1转到Activity2的时候,Acitivity1的状态变化,使用了finish()会触发onDestroy()。

logcat

这个是从Activity2转到Activity1的时候,Acitivity2的状态变化。从两次Activity的启动可以看出,流程是onCreate()->onStart()->onResume()三个方法,销毁是onPause()->onStop()->onDestroy()。另外,要往工程添加第二个Activity,需要到AndroidManifest.xml->Application那里添加Activity2。

main1.xml的源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<? 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:id = "@+id/main1.Button01"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "跳转到Activity2"
         />
     < EditText
         android:id = "@+id/EditText01"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "@+id/EditText01"
         />
     < Button
         android:id = "@+id/main1.Button02"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "跳转到外部Activity"
         />
</ LinearLayout >

main2.xml的源码

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:id = "@+id/LinearLayout01"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
     < Button
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:id = "@+id/main2.Button01"
         android:text = "返回Activity1"
         />
</ LinearLayout >

Activity1的源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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的源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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();
             }
 
         }
 
     }
}

本文转自:http://blog.csdn.net/hellogv/article/details/5992198

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值