-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
240 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 69 additions & 15 deletions
84
baselibrary/src/main/java/com/softgarden/baselibrary/BaseApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,107 @@ | ||
package com.softgarden.baselibrary; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.content.ComponentCallbacks; | ||
import android.content.Context; | ||
import android.content.res.Configuration; | ||
import android.os.Bundle; | ||
|
||
import com.scwang.smartrefresh.layout.SmartRefreshLayout; | ||
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater; | ||
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater; | ||
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator; | ||
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator; | ||
import com.scwang.smartrefresh.layout.api.RefreshFooter; | ||
import com.scwang.smartrefresh.layout.api.RefreshHeader; | ||
import com.scwang.smartrefresh.layout.api.RefreshLayout; | ||
import com.scwang.smartrefresh.layout.constant.SpinnerStyle; | ||
import com.scwang.smartrefresh.layout.footer.ClassicsFooter; | ||
import com.scwang.smartrefresh.layout.header.ClassicsHeader; | ||
import com.softgarden.baselibrary.utils.ActivityManager; | ||
|
||
|
||
/** | ||
* Created by MirkoWu on 2017/4/26 0026. | ||
*/ | ||
|
||
public class BaseApplication extends Application { | ||
private static BaseApplication instance; | ||
|
||
public static BaseApplication getInstance() { | ||
return instance; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
instance = this; | ||
} | ||
|
||
//static 代码段可以防止内存泄露 | ||
static { | ||
//设置全局的Header构建器 | ||
SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() { | ||
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() { | ||
@Override | ||
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) { | ||
layout.setPrimaryColorsId(android.R.color.white);//全局设置主题颜色 | ||
return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header,默认是 贝塞尔雷达Header | ||
} | ||
}); | ||
//设置全局的Footer构建器 | ||
SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() { | ||
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() { | ||
@Override | ||
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) { | ||
//指定为经典Footer,默认是 BallPulseFooter | ||
return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate); | ||
} | ||
}); | ||
} | ||
|
||
|
||
private static BaseApplication instance; | ||
|
||
public static BaseApplication getInstance() { | ||
return instance; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
instance = this; | ||
|
||
registerActivityLifecycle(); | ||
} | ||
|
||
|
||
/** | ||
* 全局管理Activity | ||
*/ | ||
private void registerActivityLifecycle() { | ||
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { | ||
@Override | ||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | ||
ActivityManager.getInstance().add(activity);//加入管理栈 | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityStarted(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityResumed(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityPaused(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityStopped(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityDestroyed(Activity activity) { | ||
ActivityManager.getInstance().remove(activity);//移除管理栈 | ||
} | ||
}); | ||
} | ||
|
||
} |
166 changes: 166 additions & 0 deletions
166
baselibrary/src/main/java/com/softgarden/baselibrary/utils/ActivityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.softgarden.baselibrary.utils; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* Activity管理类 | ||
* <p> | ||
* 添加/删除 建议在{@link android.app.Application#registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks)}中统一处理 | ||
* (此方法比在BaseActivity中处理要好) | ||
*/ | ||
public class ActivityManager { | ||
private static Stack<Activity> mActStack = new Stack<>(); | ||
private volatile static ActivityManager instance; | ||
|
||
public static ActivityManager getInstance() { | ||
if (instance == null) { | ||
synchronized (ActivityManager.class) { | ||
if (instance == null) { | ||
instance = new ActivityManager(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
/*** 添加 建议在Application中统一处理 */ | ||
public void add(Activity activity) { | ||
mActStack.add(activity); | ||
} | ||
|
||
/*** 移除 建议在Application中统一处理 */ | ||
public void remove(Activity activity) { | ||
if (mActStack.contains(activity)) | ||
mActStack.remove(activity); | ||
} | ||
|
||
|
||
/** | ||
* 栈内是否包含此activity | ||
* | ||
* @param cls | ||
* @return | ||
*/ | ||
public boolean isContains(Class<?> cls) { | ||
for (Activity act : mActStack) { | ||
if (act.getClass().equals(cls)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/** | ||
* 查找指定Activity 默认第一个 | ||
* | ||
* @param cls | ||
* @return 未找到则返回 null | ||
*/ | ||
public Activity find(Class cls) { | ||
return findFirst(cls); | ||
} | ||
|
||
/** | ||
* 查找指定Activity 第一个 | ||
* | ||
* @param cls | ||
* @return | ||
*/ | ||
public Activity findFirst(Class cls) { | ||
if (mActStack.isEmpty()) return null; | ||
|
||
for (int i = 0; i < mActStack.size(); i++) { | ||
Activity act = mActStack.get(i); | ||
if (act.getClass().equals(cls)) | ||
return act; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 查找指定Activity 最后一个 | ||
* | ||
* @param cls | ||
* @return | ||
*/ | ||
public Activity findLast(Class cls) { | ||
if (mActStack.isEmpty()) return null; | ||
|
||
for (int i = mActStack.size() - 1; i >= 0; i--) { | ||
Activity act = mActStack.get(i); | ||
if (act.getClass().equals(cls)) | ||
return act; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* 获取当前(即最后一个) Activity | ||
* | ||
* @return | ||
*/ | ||
public Activity getCurrent() { | ||
if (mActStack.isEmpty()) return null; | ||
|
||
return mActStack.lastElement(); | ||
} | ||
|
||
/** | ||
* 结束指定Activity | ||
* | ||
* @param activity | ||
*/ | ||
public void finish(Activity activity) { | ||
if (activity != null && !activity.isFinishing()) { | ||
activity.finish(); | ||
} | ||
// remove(activity);//这个可以不用的 记得在Application中调用 | ||
} | ||
|
||
/** | ||
* 结束指定Activity | ||
* 注:当栈中可能包含多个该Activity时,该方法会将所有的该Activity都finish | ||
* | ||
* @param cls | ||
*/ | ||
public void finish(Class cls) { | ||
if (mActStack.isEmpty()) return; | ||
|
||
for (Activity act : mActStack) { | ||
if (act.getClass().equals(cls)) { | ||
finish(act); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 结束此Activity之前的所有Activity 最终显示此Activity | ||
* | ||
* @param activity | ||
*/ | ||
public void finishUntil(Activity activity) { | ||
if (mActStack.contains(activity)) { | ||
int index = mActStack.indexOf(activity); | ||
for (int i = index + 1; i < mActStack.size(); i++) { | ||
finish(mActStack.get(i)); | ||
} | ||
} | ||
} | ||
|
||
public void finishAll() { | ||
for (Activity act : mActStack) { | ||
finish(act); | ||
} | ||
} | ||
|
||
public void exitApp() { | ||
finishAll(); | ||
System.exit(0); | ||
android.os.Process.killProcess(android.os.Process.myPid()); | ||
} | ||
} |