Android工作学习第5天之Activity的完全退出程序

注:本文大部分为网上转载,本人只是根据工作的需要略做整合!

android 完全退出应用程序

注意:1、单例模式的学习
   2、Manifest.xml,注意项目清单文件中要加上

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类 代码如下:

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.app.Application;
  5. publicclass ExitApplication extends Application {
  6. private List activityList = new LinkedList();
  7. privatestatic ExitApplication instance;
  8. private ExitApplication()
  9. {
  10. }
  11. //单例模式中获取唯一的ExitApplication实例
  12. publicstatic ExitApplication getInstance()
  13. {
  14. if(null == instance)
  15. {
  16. instance = new ExitApplication();
  17. }
  18. return instance;
  19. }
  20. //添加Activity到容器中
  21. publicvoid addActivity(Activity activity)
  22. {
  23. activityList.add(activity);
  24. }
  25. //遍历所有Activity并finish
  26. publicvoid exit()
  27. {
  28. for(Activity activity:activityList)
  29. {
  30. activity.finish();
  31. }
  32. System.exit(0);
  33. }
  34. }
 

下面的三个类IndexActivity, BActivity,CActivity是简单的例子,分别是IndexActivity–>BActivity–>CActivity的跳转顺序。在每个Activity类中onCreate()方法中调用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一个Activity界面退出应用程序时,只要调用ExitApplication.getInstance().exit()方法,就可以在任何一个Activity中完全退出应用程序。 IndexActivity 类源代码:

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. publicclass IndexActivity extends Activity {
  8. /** Called when the activity is first created. */
  9. @Override
  10. publicvoid onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. Button next=(Button)findViewById(R.id.next_to_b);
  14. next.setOnClickListener(nextClick);
  15. Button exit=(Button)findViewById(R.id.exit_main);
  16. exit.setOnClickListener(exitClick);
  17. ExitApplication.getInstance().addActivity(this);
  18. }
  19. OnClickListener nextClick=new OnClickListener() {
  20. @Override
  21. publicvoid onClick(View v) {
  22. // TODO Auto-generated method stub
  23. Intent intent=new Intent(IndexActivity.this,BActivity.class);
  24. startActivity(intent);
  25. }
  26. };
  27. OnClickListener exitClick=new OnClickListener() {
  28. @Override
  29. publicvoid onClick(View v) {
  30. // TODO Auto-generated method stub
  31. ExitApplication.getInstance().exit();
  32. }
  33. };
  34. }
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class IndexActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Button next=(Button)findViewById(R.id.next_to_b);
next.setOnClickListener(nextClick); Button exit=(Button)findViewById(R.id.exit_main);
exit.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this); } OnClickListener nextClick=new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub Intent intent=new Intent(IndexActivity.this,BActivity.class);
startActivity(intent); }
}; OnClickListener exitClick=new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}

BActivity 类源代码:

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. publicclass BActivity extends Activity {
  8. @Override
  9. protectedvoid onCreate(Bundle savedInstanceState) {
  10. // TODO Auto-generated method stub
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.b);
  13. Button next_to_c=(Button)findViewById(R.id.next_to_c);
  14. next_to_c.setOnClickListener(next_to_cClick);
  15. Button exit_b=(Button)findViewById(R.id.exit_b);
  16. exit_b.setOnClickListener(exitClick);
  17. ExitApplication.getInstance().addActivity(this);
  18. }
  19. OnClickListener next_to_cClick=new OnClickListener() {
  20. @Override
  21. publicvoid onClick(View v) {
  22. // TODO Auto-generated method stub
  23. Intent intent=new Intent(BActivity.this,CActivity.class);
  24. startActivity(intent);
  25. }
  26. };
  27. OnClickListener exitClick=new OnClickListener() {
  28. @Override
  29. publicvoid onClick(View v) {
  30. // TODO Auto-generated method stub
  31. ExitApplication.getInstance().exit();
  32. }
  33. };
  34. }
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class BActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); setContentView(R.layout.b);
Button next_to_c=(Button)findViewById(R.id.next_to_c);
next_to_c.setOnClickListener(next_to_cClick); Button exit_b=(Button)findViewById(R.id.exit_b);
exit_b.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this); } OnClickListener next_to_cClick=new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub Intent intent=new Intent(BActivity.this,CActivity.class);
startActivity(intent); }
}; OnClickListener exitClick=new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}

CActivity 类源代码:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.widget.Button;
  6. publicclass CActivity extends Activity{
  7. @Override
  8. protectedvoid onCreate(Bundle savedInstanceState) {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.c);
  12. Button exit_c=(Button)findViewById(R.id.exit_c);
  13. exit_c.setOnClickListener(exitClick);
  14. ExitApplication.getInstance().addActivity(this);
  15. }
  16. OnClickListener exitClick=new OnClickListener() {
  17. @Override
  18. publicvoid onClick(View v) {
  19. // TODO Auto-generated method stub
  20. ExitApplication.getInstance().exit();
  21. //如果只是调用以下其中的一个方法,并不会完全退出应用
  22. //android.os.Process.killProcess(android.os.Process.myPid());
  23. //System.exit(0);
  24. }
  25. };
  26. }
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class CActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); setContentView(R.layout.c); Button exit_c=(Button)findViewById(R.id.exit_c);
exit_c.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this); } OnClickListener exitClick=new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
//如果只是调用以下其中的一个方法,并不会完全退出应用
//android.os.Process.killProcess(android.os.Process.myPid());
//System.exit(0);
}
}; }

源码下载ExitActivity.zip

转载地址:http://www.yoyong.com/archives/199

上一篇:App所需申请资料


下一篇:c++单例模式为什么不在析构函数中释放静态的单例对象(转)