Activity的生命周期 二

这篇文章我们要看一下Task以及对话框风格的Activity

 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  * 1 Task的基本概念
11  * 2 Activity和Task之间的关系
12  * 3 对话框风格的Activity的使用方法,要实现这一的一个Activity,只需要在注册Activity的时候注明他就可以了
13  *         调用这样的一个Activity,原来调用的Activity不会调用onStop()方法
14  * Task 任务(栈,遵循后进先出的原则,他可以帮我们把不同应用 程序里面的Activity组织在一起)
15  * @author David
16  *
17  */
18 public class FiveLessonDemo1 extends Activity{
19     private Button button=null;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         // TODO Auto-generated method stub
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.fiveone);
26         button=(Button)findViewById(R.id.calculate);
27         button.setText("按钮1");
28         button.setOnClickListener(new ButtonListener());
29     }
30     
31     
32     class ButtonListener implements OnClickListener{
33 
34         public void onClick(View arg0) {
35             // TODO Auto-generated method stub
36             Intent intent=new Intent();
37             intent.setClass(FiveLessonDemo1.this, FiveLessonDemo4.class);
38             //    intent.setClass(FiveLessonDemo1.this, FiveLessonDemo2.class);
39             startActivity(intent);
40         }
41         
42         
43     }
44 }
 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 FiveLessonDemo2 extends Activity{
11     private Button button=null;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         // TODO Auto-generated method stub
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.fivetwo);
17         button=(Button)findViewById(R.id.calculate);
18         button.setText("按钮2");
19         button.setOnClickListener(new ButtonListener());
20     }
21     
22     
23     class ButtonListener implements OnClickListener{
24 
25         public void onClick(View v) {
26             // TODO Auto-generated method stub
27             Intent intent=new Intent();
28             intent.setClass(FiveLessonDemo2.this, FiveLessonDemo3.class);
29             startActivity(intent);
30             finish();//如果调用这个方法,那么这个Activity将被销毁,将被从堆栈中删除
31         }
32         
33         
34     }
35 }
 1 package com.example.helloworld;
 2 
 3 import com.example.helloworld.FiveLessonDemo2.ButtonListener;
 4 
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.net.Uri;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class FiveLessonDemo3 extends Activity{
14     private Button button=null;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         // TODO Auto-generated method stub
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.fivethree);
20         button=(Button)findViewById(R.id.calculate);
21         button.setText("按钮3");
22         button.setOnClickListener(new ButtonListener());
23     }
24 
25     class ButtonListener implements OnClickListener{
26 
27         public void onClick(View arg0) {
28             // TODO Auto-generated method stub
29             Uri uri=Uri.parse("smsto://0800000123");
30             Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
31             intent.putExtra("sms_body", "今天天气真好");
32             startActivity(intent);
33         }
34         
35         
36     }
37 }
 1 package com.example.helloworld;
 2 
 3 import com.example.helloworld.FiveLessonDemo2.ButtonListener;
 4 
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.net.Uri;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class FiveLessonDemo4 extends Activity{
14     private Button button=null;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         // TODO Auto-generated method stub
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.fivefour);
20         button=(Button)findViewById(R.id.calculate);
21         button.setText("确定");
22         button.setOnClickListener(new ButtonListener());
23     }
24     
25     class ButtonListener implements OnClickListener{
26 
27         public void onClick(View arg0) {
28             // TODO Auto-generated method stub
29             finish();
30         }
31         
32         
33     }
34 }

这里我们省略了样式文件,因为他们本身都比较简单,和以前一样,仅仅有一个BUTTON而已

 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         </activity>
38         <activity
39             android:name=".FourLessonDemo2"
40             android:label="dd" >
41         </activity>
42          <activity
43             android:name=".FiveLessonDemo1"
44             android:label="dd" >
45               <intent-filter>
46                 <action android:name="android.intent.action.MAIN" />
47 
48                 <category android:name="android.intent.category.LAUNCHER" />
49             </intent-filter>
50         </activity>
51          <activity
52             android:name=".FiveLessonDemo2"
53             android:label="dd" >
54         </activity>
55          <activity
56             android:name=".FiveLessonDemo3"
57             android:label="dd" >
58         </activity>
59          <activity
60             android:name=".FiveLessonDemo4"
61             android:label="dd"
62             android:theme="@android:style/Theme.Dialog" >
63         </activity>
64     </application>
65 
66 </manifest>
--------------------------------------------------------------------------------------------------------------------------------------------
顺势而为
原文地址:https://www.cnblogs.com/zhuzhenyu/p/2612722.html