android 完全退出应用程序
android android 退出应用程序, 单例模式管理Activity
引自:http://www.yoyong.com/archives/199
android 退出应用程序会调用android.os.Process.killProcess(android.os.Process.myPid())
或是System.exit(0),这只是针对第一个Activity(也就是入口的Activity)时生效。如果有A,B,C
三个Activity,而想在B 或C 中Activity 退出,调用上面的方法,往往会销毁当前的Activity 返回上
一个Activity。当然也可以逐个返回上一个Activity,直到跳转到入口的Activity,最后退出应用程
序。但这样比较麻烦,而且逐个返回的体验并不友好。
网上比较流行的方法是定义栈,写一个ExitApplication 类,利用单例模式管理Activity,在每个在
Activity 的onCreate()方法中调用ExitApplication.getInstance().addActivity(this)方法,在退
出时调用ExitApplication.getInstance().exit()方法,就可以完全退出应用程序了。
ExitApplication 类
代码如下:
view plaincopy to clipboardprint?
1 import java.util.LinkedList;
2 import java.util.List;
3
4 import android.app.Activity;
5 import android.app.Application;
6
7 public class ExitApplication extends Application {


9 private List<Activity> activityList=new LinkedList<Activity>();

10 private static ExitApplication instance;
11
12 private ExitApplication()
13 {
14 }
15 //单例模式中获取唯一的ExitApplication 实例
16 public static ExitApplication getInstance()
17 {
18 if(null == instance)
19 {
20 instance = new ExitApplication();
21 }
22 return instance;
23
24 }
25 //添加Activity 到容器中
26 public void addActivity(Activity activity)
27 {
28 activityList.add(activity);
29 }
30 //遍历所有Activity 并finish
31
32 public void exit()
33 {
34
35 for(Activity activity:activityList)
36 {
37 activity.finish();
38 }
39
40 System.exit(0);
41
42 }
43 }
下面的三个类IndexActivity, BActivity,CActivity 是简单的例子, 分别是
IndexActivity–>BActivity–>CActivity 的跳转顺序。在每个Activity 类中onCreate()方法中调
用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一个Activity 界面退
出应用程序时,只要调用ExitApplication.getInstance().exit()方法,就可以在任何一个Activity
中完全退出应用程序。
IndexActivity 类源代码:
view plaincopy to clipboardprint?
44 import android.app.Activity;
45 import android.content.Intent;
46 import android.os.Bundle;
47 import android.view.View;
48 import android.view.View.OnClickListener;
49 import android.widget.Button;
50
51 public class IndexActivity extends Activity {
52 /** Called when the activity is first created. */
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.main);
57
58 Button next=(Button)findViewById(R.id.next_to_b);
59 next.setOnClickListener(nextClick);
60
61 Button exit=(Button)findViewById(R.id.exit_main);
62 exit.setOnClickListener(exitClick);
63 ExitApplication.getInstance().addActivity(this);
64
65 }
66
67 OnClickListener nextClick=new OnClickListener() {
68
69 @Override
70 public void onClick(View v) {
71 // TODO Auto-generated method stub
72
73 Intent intent=new Intent(IndexActivity.this,BActivity.class);
74 startActivity(intent);
75
76 }
77 };
78
79 OnClickListener exitClick=new OnClickListener() {
80
81 @Override
82 public void onClick(View v) {
83 // TODO Auto-generated method stub
84 ExitApplication.getInstance().exit();
85 }
86 };
87 }
BActivity 类源代码:
view plaincopy to clipboardprint?
88 import android.app.Activity;
89 import android.content.Intent;
90 import android.os.Bundle;
91 import android.view.View;
92 import android.view.View.OnClickListener;
93 import android.widget.Button;
94
95 public class BActivity extends Activity {
96
97 @Override
98 protected void onCreate(Bundle savedInstanceState) {
99 // TODO Auto-generated method stub
100 super.onCreate(savedInstanceState);
101
102 setContentView(R.layout.b);
103 Button next_to_c=(Button)findViewById(R.id.next_to_c);
104 next_to_c.setOnClickListener(next_to_cClick);
105
106 Button exit_b=(Button)findViewById(R.id.exit_b);
107 exit_b.setOnClickListener(exitClick);
108 ExitApplication.getInstance().addActivity(this);
109
110 }
111
112 OnClickListener next_to_cClick=new OnClickListener() {
113
114 @Override
115 public void onClick(View v) {
116 // TODO Auto-generated method stub
117
118 Intent intent=new Intent(BActivity.this,CActivity.class);
119 startActivity(intent);
120
121 }
122 };
123
124 OnClickListener exitClick=new OnClickListener() {
125
126 @Override
127 public void onClick(View v) {
128 // TODO Auto-generated method stub
129 ExitApplication.getInstance().exit();
130 }
131 };
132 }
CActivity 类源代码:
view plaincopy to clipboardprint?
133 import android.app.Activity;
134 import android.os.Bundle;
135 import android.view.View;
136 import android.view.View.OnClickListener;
137 import android.widget.Button;
138
139 public class CActivity extends Activity{
140
141 @Override
142 protected void onCreate(Bundle savedInstanceState) {
143 // TODO Auto-generated method stub
144 super.onCreate(savedInstanceState);
145
146 setContentView(R.layout.c);
147
148 Button exit_c=(Button)findViewById(R.id.exit_c);
149 exit_c.setOnClickListener(exitClick);
150 ExitApplication.getInstance().addActivity(this);
151
152 }
153
154 OnClickListener exitClick=new OnClickListener() {
155
156 @Override
157 public void onClick(View v) {
158 // TODO Auto-generated method stub
159 ExitApplication.getInstance().exit();
160 //如果只是调用以下其中的一个方法,并不会完全退出应用
161 //android.os.Process.killProcess(android.os.Process.myPid());
162 //System.exit(0);
163 }
164 };
165
166 }

posted on 2012-06-08 14:56  清沁  阅读(64163)  评论(7编辑  收藏  举报