今天早上在做练习的时候R.java死活都没有自动生成出来,后来经过观察,发现res包下的文件名不可用使用大写和数字,不然就会发生这样的情况,不知道是不是一个BUG。

这篇文章将通过2个Activity来观察Activity的生命周期。

1、首先创建第一个Activity,FourLessonDemo1.java

 1 package com.example.helloworld;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 
10 public class FourLessonDemo1 extends Activity {
11 
12  private Button button;
13 
14  //当应用程序第一次被创建时,会调用onCreate方法
15  //这里需要设置布局文件,设置监听器
16  @Override
17  protected void onCreate(Bundle savedInstanceState) {
18   // TODO Auto-generated method stub
19   super.onCreate(savedInstanceState);
20   this.setContentView(R.layout.fourone);
21   button = (Button) findViewById(R.id.calculate);
22   button.setText("启动第二个Activity程序");
23   button.setOnClickListener(new buttonClick());
24   System.out.println("FirstActivity-----onCreate");
25  }
26 
27  class buttonClick implements OnClickListener {
28 
29   public void onClick(View arg0) {
30    // TODO Auto-generated method stub
31    Intent intent = new Intent();
32    intent.setClass(FourLessonDemo1.this, FourLessonDemo2.class);
33    FourLessonDemo1.this.startActivity(intent);
34   }
35 
36  }
37  //当被调用的Activity被返回(并且系统资源不够用的情况下)或调用finish方法时,调用onDestroy
38  @Override
39  protected void onDestroy() {
40   // TODO Auto-generated method stub
41 
42   System.out.println("FirstActivity-----onDestroy");
43 
44   super.onDestroy();
45  }
46  //应用程序启动另一个 Activity时,会调用onPause方法
47  //这里需要保存一些数据
48  @Override
49  protected void onPause() {
50   // TODO Auto-generated method stub
51   System.out.println("FirstActivity-----onPause");
52   super.onPause();
53  }
54  //当Activity重新返回到这个Activity且处于可见状态时,调用onRestart方法
55  @Override
56  protected void onRestart() {
57   // TODO Auto-generated method stub
58   System.out.println("FirstActivity-----onRestart");
59   super.onRestart();
60  }
61  //当获取用户焦点时会调用onResume
62  @Override
63  protected void onResume() {
64   // TODO Auto-generated method stub
65   System.out.println("FirstActivity-----onResume");
66   super.onResume();
67  }
68  //当Activity可以被用户看到时,调用onStart
69  @Override
70  protected void onStart() {
71   // TODO Auto-generated method stub
72   System.out.println("FirstActivity-----onStart");
73   super.onStart();
74  }
75  //当调用另一个Activity后,当另一个Activity完全遮挡当前Activity遮挡时,会调用onStop
76  @Override
77  protected void onStop() {
78   // TODO Auto-generated method stub
79   System.out.println("FirstActivity-----onStop");
80   super.onStop();
81  }
82 
83 }

 

2、创建第二个Activity,FourLessonDemo2.java

 1 package com.example.helloworld;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class FourLessonDemo2 extends Activity{
 7  @Override
 8  protected void onCreate(Bundle savedInstanceState) {
 9   // TODO Auto-generated method stub
10   super.onCreate(savedInstanceState);
11   System.out.println("SecondActivity-----onCreate");
12   //this.setContentView(R.layout)
13  }
14 
15  @Override
16  protected void onDestroy() {
17   // TODO Auto-generated method stub
18 
19   System.out.println("SecondActivity-----onDestroy");
20 
21   super.onDestroy();
22  }
23 
24  @Override
25  protected void onPause() {
26   // TODO Auto-generated method stub
27   System.out.println("SecondActivity-----onPause");
28   super.onPause();
29  }
30 
31  @Override
32  protected void onRestart() {
33   // TODO Auto-generated method stub
34   System.out.println("SecondActivity-----onRestart");
35   super.onRestart();
36  }
37 
38  @Override
39  protected void onResume() {
40   // TODO Auto-generated method stub
41   System.out.println("SecondActivity-----onResume");
42   super.onResume();
43  }
44 
45  @Override
46  protected void onStart() {
47   // TODO Auto-generated method stub
48   System.out.println("SecondActivity-----onStart");
49   super.onStart();
50  }
51 
52  @Override
53  protected void onStop() {
54   // TODO Auto-generated method stub
55   System.out.println("SecondActivity-----onStop");
56   super.onStop();
57  }
58 }

 

3、创建第一个Activity的布局文件,fourone.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/calculate"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content" />
11 
12 </LinearLayout>

 

4、注册Activity

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 2     package="com.example.helloworld"
 3     android:versionCode="1"
 4     android:versionName="1.0" >
 5 
 6     <uses-sdk
 7         android:minSdkVersion="4"
 8         android:targetSdkVersion="15" />
 9 
10     <application
11         android:icon="@drawable/ic_launcher"
12         android:label="@string/app_name"
13         android:theme="@style/AppTheme" >
14         <activity
15             android:name=".MainActivity"
16             android:label="@string/title_activity_main" >
17         </activity>
18         <activity
19             android:name=".SecondLessonDemo1"
20             android:label="@string/other" >
21         </activity>
22         <activity
23             android:name=".SecondLessonDemo2"
24             android:label="dd" >
25         </activity>
26         <activity
27             android:name=".ThridLessonDemo1"
28             android:label="dd2" >
29         </activity>
30         <activity
31             android:name=".ThridLessonDemo2"
32             android:label="dd3" >
33         </activity>
34         <activity
35             android:name=".FourLessonDemo1"
36             android:label="dd" >
37             <intent-filter>
38                 <action android:name="android.intent.action.MAIN" />
39 
40                 <category android:name="android.intent.category.LAUNCHER" />
41             </intent-filter>
42         </activity>
43         <activity
44             android:name=".FourLessonDemo2"
45             android:label="dd" >
46         </activity>
47     </application>
48 
49 </manifest>

我们这里打印出的调试信息,需要打开DDMS窗口中查看

posted on 2012-07-27 15:40  振宇爸爸  阅读(169)  评论(0编辑  收藏  举报