public abstract class BaseActivity extends Activity {
/**
* 记录处于前台的Activity
*/
private static BaseActivity mForegroundActivity = null;
/**
* 记录所有活动的Activity
*/
private static final List mActivities = new LinkedList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initFindViewById();
initData();
initEvent();
// initActionBar();
}
@Override
protected void onResume() {
mForegroundActivity = this;
super.onResume();
}
@Override
protected void onPause() {
mForegroundActivity = null;
super.onPause();
}
abstract protected void initView();
abstract protected void initData();
protected void initActionBar() {
}
/**
* 关闭所有Activity
*/
public static void finishAll() {
List copy;
synchronized (mActivities) {
copy = new ArrayList(mActivities);
}
for (BaseActivity activity : copy) {
activity.finish();
}
}
/**
* 关闭所有Activity,除了参数传递的Activity
*/
public static void finishAll(BaseActivity except) {
List copy;
synchronized (mActivities) {
copy = new ArrayList(mActivities);
}
for (BaseActivity activity : copy) {
if (activity != except)
activity.finish();
}
}
/**
* 是否有启动的Activity
*/
public static boolean hasActivity() {
return mActivities.size() > 0;
}
/**
* 获取当前处于前台的activity
*/
public static BaseActivity getForegroundActivity() {
return mForegroundActivity;
}
/**
* 获取当前处于栈顶的activity,无论其是否处于前台
*/
public static BaseActivity getCurrentActivity() {
List copy;
synchronized (mActivities) {
copy = new ArrayList(mActivities);
}
if (copy.size() > 0) {
return copy.get(copy.size() - 1);
}
return null;
}
protected void initFindViewById() {
}
protected void initEvent() {
}
/**
* 退出应用
*/
public void exitApp() {
finishAll();
android.os.Process.killProcess(android.os.Process.myPid());
}
}