前言
单例设计模式(一) - 定义、volatile关键字好处
单例设计模式(二) - 常见几种写法
单例设计模式(三) - 强大的Activity的管理
1. 概述
前两篇文章已经讲解了单例设计模式的定义、volatile关键字的好处、及常见单例设计模式的几种写法,那么这篇文章就记录下单例设计模式使用场景 —— 强大的Activity的管理。
2. 单例使用场景
如上图所示
场景一:
1>:点击收藏,如果没有登录,先跳转到登录页面,如果没有注册,就去注册;
2>:注册成功并且登录成功后,然后保存用户信息;
3>:关闭注册页面及登录页面;
场景二:
单点登录
如果后台通知,该账号已经被挤下线了,那么不管你在哪个页面,都需要弹出dialog,而且必须是Activity的上下文,是否需要在每个Activity中写代码,或者说直接写到 BaseActivity中即可;
以上都是单例设计模式的使用场景。
对于场景1:定义一个ActivityManager管理类,将其写成单例设计模式,并且用Stack栈,类型是Activity,管理整个Activity,然后在每个Activity的onCreate()中调用 ActivityManager的 attach()方法,把Activity添加到栈中,在每个Activity的onDestroy()方法中,调用 detach()方法,把Activity从栈中移除,代码如下;
以下场景是:从MainActivity跳转到 LoginActivity,然后跳转到RegisterActivity,最终关闭RegisterActivity和LoginActivity,回到MainActivity
1>:ActivityManager管理类代码如下:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/5/6 16:30
* Version 1.0
* Params:
* Description: Activity的栈管理
*/
public class ActivityManager {
private static volatile ActivityManager mInstance;
// 集合 有 List LinkedList Stack ?
// 由于删除和添加比较多,所以可以使用 Stack栈
private Stack<Activity> mActivities;
private ActivityManager(){
mActivities = new Stack<>();
}
// 双重校验
public static ActivityManager getInstance() {
if (mInstance == null) {
synchronized (ActivityManager.class) {
if (mInstance == null) {
mInstance = new ActivityManager();
}
}
}
return mInstance;
}
/**
* 添加统一管理
*/
public void attach(Activity activity){
mActivities.add(activity);
}
/**
* 移除解绑 - 防止内存泄漏
*/
public void detach(Activity detachActivity){
// for循环 一边循环一边移除会出问题 ,
/*for (Activity activity : mActivities) {
if(activity == detachActivity){
mActivities.remove(activity);
}
}*/
int size = mActivities.size();
for (int i = 0; i < size; i++) {
Activity activity = mActivities.get(i);
if (activity == detachActivity) {
mActivities.remove(i);
i--;
size--;
}
}
}
/**
* 关闭当前的 Activity
*/
public void finish(Activity finishActivity){
// for循环 一边循环一边移除会出问题 ,
/*for (Activity activity : mActivities) {
if(activity == finishActivity){
mActivities.remove(activity);
activity.finish();
}
}
*/
int size = mActivities.size();
for (int i = 0; i < size; i++) {
Activity activity = mActivities.get(i);
if (activity == finishActivity) {
mActivities.remove(i);
activity.finish();
i--;
size--;
}
}
}
/**
* 根据Activity的类名关闭 Activity
*/
public void finish(Class<? extends Activity> activityClass){
// for循环 一边循环一边移除会出问题 ,
/*for (Activity activity : mActivities) {
if(activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())){
mActivities.remove(activity);
activity.finish();
}
}*/
int size = mActivities.size();
for (int i = 0; i < size; i++) {
Activity activity = mActivities.get(i);
if (activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())) {
mActivities.remove(i);
activity.finish();
i--;
size--;
}
}
}
/**
* 退出整个应用
*/
public void exitApplication(){
}
/**
* 获取当前的Activity(最前面)
*/
public Activity currentActivity(){
return mActivities.lastElement();
}
}
2>:MainActivity代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.getInstance().attach(this);
setContentView(R.layout.activity_main);
setTitle("MainActivity");
}
public void click(View view){
Intent intent = new Intent(this,LoginActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
ActivityManager.getInstance().detach(this);
super.onDestroy();
}
}
3>:LoginActivity代码如下:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/5/6 16:59
* Version 1.0
* Params:
* Description:
*/
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.getInstance().attach(this);
setContentView(R.layout.activity_main);
setTitle("LoginActivity");
}
public void click(View view){
Intent intent = new Intent(this,RegisterActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
ActivityManager.getInstance().detach(this);
super.onDestroy();
}
}
4>:RegisterActivity代码如下:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/5/6 16:47
* Version 1.0
* Params:
* Description:
*/
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.getInstance().attach(this);
setContentView(R.layout.activity_main);
setTitle("RegisterActivity");
}
public void click(View view){
// 不光要关闭自己还要关闭 LoginActivity 让其回到主页
ActivityManager.getInstance().finish(this);
ActivityManager.getInstance().finish(LoginActivity.class);
}
@Override
public void onDestroy() {
ActivityManager.getInstance().detach(this);
super.onDestroy();
}
}
代码已上传至github:
https://github.com/shuai999/Architect_day8.git