标签:
Android应用程序的不能自己控制他们自己的进程,但是Android运行时可以管理每个进程。因此正确的理解Activity的生存期,对于保证用户有个良好的体验有着很大的帮助。
非活动状态 Activity被终止之后就是处于非活动状态。此状态的Activity已经被移除Activity栈。若要显示此状态的Activity,需要重新启动该Activity。
3.MainActivity
1 package com.example.zhouy.android4exercise;
2
3 import android.app.Activity;
4 import android.content.DialogInterface;
5 import android.content.Intent;
6 import android.os.Bundle;
7 import android.os.PersistableBundle;
8 import android.support.v7.app.AlertDialog;
9 import android.util.Log;
10 import android.view.View;
11 import android.widget.Button;
12
13 public class MainActivity extends Activity implements View.OnClickListener{
14
15 private Button mAlertBtn = null;
16
17 private Button mJumpBtn = null;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23 mAlertBtn = (Button)findViewById(R.id.alert_dialog);
24 mAlertBtn.setOnClickListener(this);
25 mJumpBtn = (Button)findViewById(R.id.jump_to_another_activity);
26 mJumpBtn.setOnClickListener(this);
27 Log.v("Tag","onCreate()");
28 }
29
30 private void alertDialog(){
31 //利用Activity进行弹出对话框时,Activity的生命周期不会改变。
32 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
33 builder.setTitle("对话框标题");
34 builder.setMessage("对话框内容");
35 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
36 @Override
37 public void onClick(DialogInterface dialogInterface, int i) {
38 Log.v("Tag","Ok");
39 }
40 });
41 builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
42 @Override
43 public void onClick(DialogInterface dialogInterface, int i) {
44 Log.v("Tag","Cancel");
45 }
46 });
47 builder.create();
48 builder.show();
49
50 }
51
52 private void gotoNextActivity(){
53 Intent intent = new Intent(this,NextActivity.class);
54 startActivity(intent);
55 }
56 @Override
57 protected void onRestart() {
58 super.onRestart();
59 Log.v("Tag","onReStart()");
60 }
61
62 @Override
63 protected void onRestoreInstanceState(Bundle savedInstanceState) {
64 super.onRestoreInstanceState(savedInstanceState);
65 Log.v("Tag","onRestoreInstanceState");
66 }
67
68 @Override
69 protected void onStart() {
70 super.onStart();
71 Log.v("Tag","onStart()");
72
73 }
74
75 @Override
76 protected void onResume() {
77 super.onResume();
78 Log.v("Tag","onResume()");
79
80 }
81
82
83 @Override
84 protected void onPause() {
85 super.onPause();
86 Log.v("Tag","onPause()");
87
88 }
89
90 @Override
91 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
92 super.onSaveInstanceState(outState, outPersistentState);
93 Log.v("Tag","onSaveInstanceState");
94 }
95
96 @Override
97 protected void onStop() {
98 super.onStop();
99 Log.v("Tag","onStop()");
100
101 }
102
103 @Override
104 protected void onDestroy() {
105 super.onDestroy();
106 Log.v("Tag","onDestory()");
107
108 }
109
110 @Override
111 public void onClick(View view) {
112 int id = view.getId();
113 if(id == R.id.alert_dialog){
114 alertDialog();
115 }else if(id == R.id.jump_to_another_activity){
116 gotoNextActivity();
117 }
118 }
119 }
120
4.activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context="com.example.zhouy.android4exercise.MainActivity"> 11 <Button 12 android:id="@+id/alert_dialog" 13 android:layout_alignParentTop="true" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="弹出对话框"/> 17 <Button 18 android:id="@+id/jump_to_another_activity" 19 android:layout_below="@id/alert_dialog" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="跳转到另一个Activity"/> 23 24 </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/endeavor1209/p/5677622.html