android 使用多个Intent 进行activity跳转 而没有finish的情况,如何直接退出系统

android 使用多个Intent 进行activity跳转 而没有finish的情况,如何直接退出系统

就是 N 个 Activity 跳转 没finish(); 最后一个 Activity单击事件 如何推出整个 程序?

很简单:只要在最后一个需要finish掉之前所有的avtivity 的Intent 上加上这样一句话                                                                       

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//注意本行的FLAG设置

参考:http://www.open-open.com/home/space-37924-do-blog-id-5805.html

Android-完全退出当前应用程序的四种方法

 

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,如果在B中直接finish(), 接下里显示的是A。在B中如何关闭整个Android应用程序呢?本人总结了几种比较简单的实现方法。

1. Dalvik VM的本地方法

 android.os.Process.killProcess(android.os.Process.myPid())    //获取PID 
 System.exit(0);   //常规java、c#的标准退出法,返回值为0代表正常退出

2. 任务管理器方法

  首先要说明该方法运行在Android 1.5 API Level为3以上才可以,同时需要权限

 ActivityManager am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE);
 am.restartPackage(getPackageName()); 
 系统会将,该包下的 ,所有进程,服务,全部杀掉,就可以杀干净了,要注意加上 
 <uses-permission android:name=\"android.permission.RESTART_PACKAGES\"></uses-permission>

3. 根据Activity的声明周期

3.  我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标 志     Intent.FLAG_ACTIVITY_CLEAR_TOP,这样开启B时将会清除该进程空间的所有Activity。

在A窗口中使用下面的代码调用B窗口

Intent intent = new Intent(); 
intent.setClass(Android123.this, CWJ.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置 
startActivity(intent);

接下来在B窗口中需要退出时直接使用finish方法即可全部退出。

4.自定义一个Actiivty 栈,道理同上,不过利用一个单例模式的Activity栈来管理所有Activity。并提供退出所有Activity的方法。代码如下:

view source

print   ?

01   public class ScreenManager {

02 private static Stack<Activity> activityStack;

03 private static ScreenManager instance;

04 private  ScreenManager(){

05 }

06 public static ScreenManager getScreenManager(){

07  if(instance==null){

08   instance=new ScreenManager();

09  }

10  return instance;

11 }

12//退出栈顶Activity

13 public void popActivity(Activity activity){

14  if(activity!=null){

15   activity.finish();

16   activityStack.remove(activity);

17   activity=null;

18  }

19 }

20

21//获得当前栈顶Activity

22 public Activity currentActivity(){

23  Activity activity=activityStack.lastElement();

24  return activity;

25 }

26

27//将当前Activity推入栈中

28 public void pushActivity(Activity activity){

29  if(activityStack==null){

30   activityStack=new Stack<Activity>();

31  }

32  activityStack.add(activity);

33 }

34 //退出栈中所有Activity

35 public void popAllActivityExceptOne(Class cls){

36  while(true){

37   Activity activity=currentActivity();

38   if(activity==null){

39    break;

40   }

41   if(activity.getClass().equals(cls) ){

42    break;

43   }

44   popActivity(activity);

45  }

46 }

47}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包括 2011=上次登录的用户名 2131=缓存 2132=历史记录 2133=表单历史 2134=Cookies 2151=缓存 2152=历史 2153=Cookies 2905=Mozilla Firefox 2906=Opera 2135=Firefox 3 历史 2171=Microsoft Office - 最近打开的文件列表 2172=Windows 媒体播放器 - 最近打开的文件列表 2173=Windows Live Mail - 邮件文件 (*.eml) 缓存 2907=其它微软应用程序 108=卸载 USB 设备 2055=已安装的 USB 设备 2012=用户助手列表 2174=Visual Studio - 最近使用的文件列表 2175=Visual Studio - 最近使用的工程列表 2176=Acrobat Reader 3012=包含您在资源管理器中打开过的程序和快捷方式的相关信息 3055=已安装的 USB 设备 CleanAfterMe v1.25 Copyright (c) 2008 Nir Sofer Web site: http://www.nirsoft.net Description =========== CleanAfterMe allows you to easily clean files and Registry entries that are automatically created by the Windows operating system during your regular computer work. With CleanAfterMe, you can clean the cookies/history/cache/passwords of Internet Explorer, the 'Recent' folder, the Registry entries that record the last opened files, the temporary folder of Windows, the event logs, the Recycle Bin, and more. CleanAfterMe is a portable application. This means that you can run it without any installation and without leaving any trace in the Registry. You can also run this program from a USB flash drive: In order to do that, simply copy the executable file of CleanAfterMe (CleanAfterMe.exe) into the USB flash drive, and run it from there.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值