APP完全退出
Intent intent = new Intent();
intent.setClass(Android123.this, CWJ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置
startActivity(intent);
|
4.自定义一个Actiivty 栈,道理同上,不过利用一个单例模式的Activity栈来管理所有Activity。并提供退出所有Activity的方法。
整个应用的生命同期中维护一个List集合,极易产生内存堆积,处理不好就是内存堆积和溢出的现象,如果未关闭的Activity比较多的话,退出时会明显有一个卡的过程,用户体验非常好.
代码如下:
public class ScreenManager {
private static Stack<Activity> activityStack; private static ScreenManager instance; private ScreenManager(){ } public static ScreenManager getScreenManager(){ if(instance==null){ instance=new ScreenManager(); } return instance; } //退出栈顶Activity public void popActivity(Activity activity){ if(activity!=null){ activity.finish(); activityStack.remove(activity); activity=null; } } //获得当前栈顶Activity public Activity currentActivity(){ Activity activity=activityStack.lastElement(); return activity; } //将当前Activity推入栈中 public void pushActivity(Activity activity){ if(activityStack==null){ activityStack=new Stack<Activity>(); } activityStack.add(activity); } //退出栈中所有Activity public void popAllActivityExceptOne(Class cls){ while(true){ Activity activity=currentActivity(); if(activity==null){ break; } if(activity.getClass().equals(cls) ){ break; } popActivity(activity); } } } |
package com.example.appexitdemo;
import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
//自定义一个广播接收器,用来接收应用程序退出广播.
public class ExitAppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (context != null) {
if (context instanceof Activity) {
((Activity) context).finish();
} else if (context instanceof FragmentActivity) {
((FragmentActivity) context).finish();
} else if (context instanceof Service) {
((Service) context).stopSelf();
}
}
}
}
|
package com.example.appexitdemo;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
//应用程序中所有Activity的基类
public class BaseActivity extends Activity {
private ExitAppReceiver exitReceiver = new ExitAppReceiver();
// 自定义退出应用Action,实际应用中应该放到整个应用的Constant类中.
private static final String EXIT_APP_ACTION = "com.micen.exit_app";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerExitReceiver();
}
private void registerExitReceiver() {
IntentFilter exitFilter = new IntentFilter();
exitFilter.addAction(EXIT_APP_ACTION);
registerReceiver(exitReceiver, exitFilter);
}
private void unRegisterExitReceiver() {
unregisterReceiver(exitReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
unRegisterExitReceiver();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
}
|
Intent intent = new Intent();
intent.setAction(EXIT_APP_ACTION);
sendBroadcast(intent);
|
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。