刚才看了一个很好的项目里面条理框框非常的清晰,感觉写的BaseActivity和BaseApplication很棒,先介绍一下他里面的application存放的都是什么?程序员第一笔的时候应该怎么来定义里面的东西。
首先定义一个对外提供整个应用生命周期的Context,这个不用多说了。还有寄存整个应用Activity的栈,当前Activity的弱引用,防止内存泄露,还有一个好处就是循环退出返回,避免不必要的麻烦。之前activity销毁的时候都是用的finish,大家在做项目的时候有没有遇到多次返回的情况把自己绕晕的
在BlackBerry中,提供了一个管理Screen的栈,用来从任何地方来关闭位于最上一层的Screen,使 UiApplication.getUiApplication().getActiveScreen()来得到位于最上一层的Screen的实例,并且使用UiApplication.getUiApplication().popScreen()来关闭一个Screen或关闭当前最上一层的Screen。
但是Android却未提供相应的功能,只能在一个Activity的对象里面调用finish来关闭自己,不能关闭其他的Activity。
比如我们想实现一个功能从屏幕A->屏幕B—>屏幕C—>屏幕D,然后在在转到屏幕D之前将屏幕B和C关闭,在屏幕B和屏幕C界面点击会退按钮都可以回退到上一个屏幕,但是在屏幕D上点击会退按钮让其回退到A,此外在一些循环跳转的界面上如果不在合适的地方将一些不需要的屏幕关闭,那么经过多次跳转后回导致内存溢出。对此我们可以设计一个全局的Activity栈,使用这个栈来管理Activity。
BaseApplication的书写参考:
public class MApplication extends Application {
/**对外提供整个应用生命周期的Context**/
private static Context instance;
/**整个应用全局可访问数据集合**/
private static Map<String, Object> gloableData = new HashMap<String, Object>();
/***volley提供的异步图片Loader**/
private static ImageLoader mImageLoader = null;
/***volley提供的异步图片缓存**/
private final NetworkImageCache imageCacheMap = new NetworkImageCache();
/***寄存整个应用Activity**/
private final Stack<WeakReference<Activity>> activitys = new Stack<WeakReference<Activity>>();
/**
* 对外提供Application Context
* @return
*/
public static Context gainContext() {
return instance;
}
public void onCreate() {
super.onCreate();
instance = this;
//初始化请求队列
RequestManager.getInstance().init(MApplication.this);
mImageLoader = new ImageLoader(RequestManager.getInstance().getRequestQueue(), imageCacheMap);
//初始化图片加载器
initImageLoader(getApplicationContext());
}
/**
* 获取网络是否已连接
* @return
*/
public static boolean isNetworkReady(){
return ToolNetwork.getInstance().init(instance).isConnected();
}
/**
* 获取图片异步加载器
* @return ImageLoader
*/
public static ImageLoader getImageLoader() {
return mImageLoader;
}
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.discCacheSize(50 * 1024 * 1024) // 50 Mb
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
// Initialize ImageLoader with configuration.
com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
}
/**Application数据操作API(开始)*/
/**
* 往Application放置数据(最大不允许超过5个)
* @param strKey 存放属性Key
* @param strValue 数据对象
*/
public static void assignData(String strKey, Object strValue) {
if (gloableData.size() > 5) {
throw new RuntimeException("超过允许最大数");
}
gloableData.put(strKey, strValue);
}
/**
* 从Applcaiton中取数据
* @param strKey 存放数据Key
* @return 对应Key的数据对象
*/
public static Object gainData(String strKey) {
return gloableData.get(strKey);
}
/*
* 从Application中移除数据
*/
public static void removeData(String key){
if(gloableData.containsKey(key)) gloableData.remove(key);
}
/**Application数据操作API(结束)*/
/**Application中存放的Activity操作(压栈/出栈)API(开始)**/
/**
* 将Activity压入Application栈
* @param task 将要压入栈的Activity对象
*/
public void pushTask(WeakReference<Activity> task) {
activitys.push(task);
}
/**
* 将传入的Activity对象从栈中移除
* @param task
*/
public void removeTask(WeakReference<Activity> task) {
activitys.remove(task);
}
/**
* 根据指定位置从栈中移除Activity
* @param taskIndex Activity栈索引
*/
public void removeTask(int taskIndex) {
if (activitys.size() > taskIndex)
activitys.remove(taskIndex);
}
/**
* 将栈中Activity移除至栈顶
*/
public void removeToTop() {
int end = activitys.size();
int start = 1;
for (int i = end - 1; i >= start; i--) {
if (!activitys.get(i).get().isFinishing()) {
activitys.get(i).get().finish();
}
}
}
/**
* 移除全部(用于整个应用退出)
*/
public void removeAll() {
//finish所有的Activity
for (WeakReference<Activity> task : activitys) {
if (!task.get().isFinishing()) {
task.get().finish();
}
}
}
/*******************************************Application中存放的Activity操作(压栈/出栈)API(结束)*****************************************/
}
BaseActivity的书写借鉴
public class BaseActivity extends AppCompatActivity {
private final String TAG = BaseActivity.class.getName();
private Toast toast = null;
private ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* @param context 上下文对象
* @param start 其实activity
* @param target 目标activity
*/
public void jumpToActivity(Context context, Class start, Class target) {
Intent intent = new Intent(context, target);
//------用户返回上一级activity
if (target == CarFindingActivity.class) {
intent.putExtra("activity", start);
}
context.startActivity(intent);
finish();
}
public void jumpToActivity(Context context, Class target) {
Intent intent = new Intent(context, target);
//------用户返回上一级activity
context.startActivity(intent);
finish();
}
/**
* @param context
* @param text
*/
public void showToastLong(Context context, String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
} else {
toast.setText(text);
}
toast.show();
}
public void showToastLong(String text) {
if (toast == null) {
toast = Toast.makeText(BaseActivity.this, text, Toast.LENGTH_LONG);
} else {
toast.setText(text);
}
toast.show();
}
/**
* @param context
* @param text
*/
public void showToastShort(Context context, String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
}
toast.show();
}
public void showToastShort(String text) {
if (toast == null) {
toast = Toast.makeText(BaseActivity.this, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
}
toast.show();
}
//-----显示ProgressDialog
public void showProgress(String message) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(BaseActivity.this, ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);//设置点击不消失
}
if (progressDialog.isShowing()) {
progressDialog.setMessage(message);
} else {
progressDialog.setMessage(message);
progressDialog.show();
}
}
//------取消ProgressDialog
public void removeProgress(){
if (progressDialog==null){
return;
}
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
}
/**
*
*/
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
removeProgress();//让progressdialog 消失
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}