- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseActivity.java
More file actions
Latest commit
105 lines (91 loc) · 2.66 KB
/
Copy pathBaseActivity.java
File metadata and controls
105 lines (91 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
packagecom.houmananger.root.activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importcom.gaoyuan4122.appmarket.utils.UIUtil;
importjava.util.LinkedList;
importjava.util.List;
/**
* Created by GAOYUAN on 2015/7/7.
* 所有 Activity 的父类, 统一的初始化页面的方法, 获取当前前台 Activity 的方法.
* 原先的代码是继承 ActionBarActivity, 后此类过时, 所以改为 AppCompatActivity, 效果一样
*/
publicclassBaseActivityextendsAppCompatActivity {
/**
* 记录前台 Activity
*/
publicstaticBaseActivitysForegroundActivity;
/**
* 记录所有没有销毁的 Activity
*/
publicLinkedList<BaseActivity> mActivities = newLinkedList<BaseActivity>();
/**
* 获取前台 Activity
*/
publicstaticBaseActivitygetForegroundActivity() {
returnsForegroundActivity;
}
/**
* 启动一个 Activity
*
* @param intent
*/
publicstaticvoidstartAnActivity(Intentintent) {
if (sForegroundActivity != null) {
sForegroundActivity.startActivity(intent);
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UIUtil.getAppContext().startActivity(intent);
}
}
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initActionBar();
mActivities.add(this);
}
/**
* 填充布局, 需要子类调用 setContentView, 做控件的初始化和设置事件
*/
protectedvoidinitView() {
}
/**
* 设置 ActionBar, 需要子类重写
*/
protectedvoidinitActionBar() {
}
@Override
protectedvoidonResume() {
sForegroundActivity = this;
super.onResume();
}
@Override
protectedvoidonPause() {
if (sForegroundActivity == this) {
sForegroundActivity = null;
}
super.onPause();
}
@Override
protectedvoidonDestroy() {
mActivities.remove(this);
super.onDestroy();
}
/**
* 结束所有没有销毁的 Activity, 结束当前进程
*/
publicvoidkillAll() {
// 复制了一份mActivities 集合
List<BaseActivity> copy;
synchronized (mActivities) {
copy = newLinkedList<BaseActivity>(mActivities);
}
for (BaseActivityactivity : copy) {
// 结束当前 Activity, 也可以使用广播
activity.finish();
}
// 杀死当前的进程
android.os.Process.killProcess(android.os.Process.myPid());
}
}