感觉ActivityStack没啥可以讲的主要目的就是将activity存入一个自己定义的栈中再根据自己的需要处理activity中的信息
代码如下
public class ActivityStack {
private final static String TAG = "ActivityHolder";
private List<FragmentActivity> activityList = new ArrayList<FragmentActivity>();
;
private static ActivityStack instance;
private ActivityStack() {
}
public static synchronized ActivityStack getInstance() {
if (instance == null) {
instance = new ActivityStack();
}
return instance;
}
/**
* add the activity in to a list end
*
* @param activity
*/
public void addActivity(FragmentActivity activity) {
try {
if (activity != null && activityList != null) {
int size = activityList.size();
if (checkActivityIsVasivle(activity)) {
removeActivity(activity);
activityList.add(activityList.size(), activity);
} else {
activityList.add(activity);
}
size = activityList.size();
for (int i = 0; i < size; i++) {
LogUtils.i("addActivity ==[" + i + "]" + " " + activityList.get(i));
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity() {
Activity activity = activityList.get(0);
return activity;
}
/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishCurActivity() {
Activity activity = currentActivity();
activityList.remove(0);
activity.finish();
}
/**
* finish all the activity in the list.
* <p/>
* <p/>
* the activity calling this method hold the context
*/
public void finishAllActivity() {
if (activityList != null) {
int size = activityList.size();
for (int i = size - 1; i >= 0; i--) {
FragmentActivity activity = activityList.get(i);
if (activity != null) {
activity.finish();
}
LogUtils.i("finishAllActivity ==[" + i + "]" + " " + activity);
activityList.remove(activity);
// activityList.clear();
}
}
}
/**
* finish all the activity in the list.
* <p/>
* <p/>
* the activity calling this method hold the context
*/
public void finishOtherActivity(FragmentActivity activityCurrent) {
if (activityList != null) {
int size = activityList.size();
for (int i = size - 1; i >= 0; i--) {
FragmentActivity activity = activityList.get(i);
if (activity != null && activity != activityCurrent) {
if (activity != null) {
activity.finish();
}
LogUtils.i("finishAllActivity ==[" + i + "]" + " " + activity);
activityList.remove(activity);
}
}
}
}
/**
* remove the finished activity in the list.
*
* @param activity the activity is removed from activityList
*/
public void removeActivity(FragmentActivity activity) {
try {
if (activityList != null) {
activityList.remove(activity);
LogUtils.i("removeActivity==" + " " + activity + "activityList.size===" + activityList.size());
}
} catch (Exception e) {
LogUtils.e("removeActivity" + e.getMessage());
}
}
public void pop(Class<? extends FragmentActivity> activity) {
for (Activity a : activityList) {
LogUtils.i(a.getClass().getName() + "--->" + activity.getName());
if (a.getClass().getName().equals(activity.getName())) {
if (!a.isFinishing()) {
a.finish();
}
}
}
activityList.remove(activity);
}
public boolean checkActivityIsVasivle(FragmentActivity activity) {
LogUtils.i(" " + activityList.contains(activity));
return activityList.contains(activity);
}
/**
* <p>
* 功能 干掉除home外的所有activity
* </p>
*
* @author sss 时间 2013年12月11日 上午5:38:59
*/
public void finishWorkActivity() {
Activity act = null;
for (int index = 0; index < activityList.size(); index++) {
act = activityList.remove(index);
if (act == null) {
continue;
}
// if(act.getClass()==HomeActivity.class){
// continue;
// }
act.finish();
}
}
public static boolean isTopActivity(FragmentActivity activity) {
boolean isTop = false;
ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn.getClassName().contains(activity.getClass().getSimpleName())) {
isTop = true;
}
return isTop;
}
}
与之关联的还有一个处理app异常的类代码如下
public class AppException extends Exception implements UncaughtExceptionHandler {
private static final long serialVersionUID = -6262909398048670705L;
private String message;
private Thread.UncaughtExceptionHandler mDefaultHandler;
private AppException() {
super();
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}
public AppException(String message, Exception excp) {
super(message, excp);
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* 获取APP异常崩溃处理对象
*
* @return
*/
public static AppException getAppExceptionHandler() {
return new AppException();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
Log.e("=uncaughtException=","uncaughtException");
if (InnoFarmApplication.getAppContext()!=null){
TCAgent.onError(InnoFarmApplication.getAppContext(), ex);
}
mDefaultHandler.uncaughtException(thread, ex);
}
}
/**
* 自定义异常处理
*
* @param ex
* @return true:处理了该异常信息;否则返回false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
final String errLog = ex.toString();
final Activity activity = ActivityStack.getInstance().currentActivity();
if (activity == null) {
return false;
}
new Thread() {
@Override
public void run() {
Looper.prepare();
new AlertDialog.Builder(activity).setTitle("提示")
.setCancelable(false).setMessage(errLog)
.setNeutralButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
AppManager.getAppManager().finishAllActivity();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
}).create().show();
Looper.loop();
}
}.start();
return true;
}
}
这样的话我们就能判断异常位置我们的BaseActivity是这样处理的
public abstract class BaseActivity extends FragmentActivity {
// 管理所有打开的activity
// static 和对象没有关系 和类有关系
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
init();
initView();
ActivityStack.getInstance().addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityStack.getInstance().removeActivity(this);
}
/**
* 初始化
*/
protected abstract void init();
/**
* 初始化view界面
*/
protected abstract void initView() ;
}