本文实例讲述了AndroID开发之Activity管理工具类。分享给大家供大家参考,具体如下:
这个工具类是对Activity的一些管理,非常适用
package com.maobang.imsdk.util;import java.util.Stack;import androID.app.Activity;import androID.app.ActivityManager;import androID.content.Context;import androID.graphics.drawable.Drawable;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.webkit.WebVIEw;import androID.Widget.ImageVIEw;import androID.Widget.ListVIEw;/** * Activity管理类 * Created by administrator on 2016/11/24. */public class ActivityPageManager { private static Stack<Activity> activityStack; private static ActivityPageManager instance; /** * constructor */ private ActivityPageManager() { } /** * get the AppManager instance,the AppManager is singleton. */ public static ActivityPageManager getInstance() { if (instance == null) { instance = new ActivityPageManager(); } return instance; } /** * add Activity to Stack */ public voID addActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.add(activity); } /** * remove Activity from Stack */ public voID removeActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.remove(activity); } /** * get current activity from Stack */ public Activity currentActivity() { Activity activity = activityStack.lastElement(); return activity; } /** * finish current activity from Stack */ public voID finishActivity() { Activity activity = activityStack.lastElement(); finishActivity(activity); } /** * finish the Activity */ public voID finishActivity(Activity activity) { if (activity != null) { activityStack.remove(activity); activity.finish(); activity = null; } } /** * finish the Activity */ public voID finishActivity(Class<?> cls) { for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * finish all Activity */ public voID finishAllActivity() { if(activityStack!=null&&activityStack.size()>0) { for (int i = 0,size = activityStack.size(); i < size; i++) { if (null != activityStack.get(i)) { activityStack.get(i).finish(); } } activityStack.clear(); } } /** * release all resourse for vIEw * @param vIEw */ public static voID unbindReferences(VIEw vIEw) { try { if (vIEw != null) { vIEw.destroyDrawingCache(); unbindVIEwReferences(vIEw); if (vIEw instanceof VIEwGroup){ unbindVIEwGroupReferences((VIEwGroup) vIEw); } } } catch (Throwable e) { // whatever exception is thrown just ignore it because a crash is // always worse than this method not doing what it's supposed to do } } private static voID unbindVIEwGroupReferences(VIEwGroup vIEwGroup) { int nrOfChildren = vIEwGroup.getChildCount(); for (int i = 0; i < nrOfChildren; i++) { VIEw vIEw = vIEwGroup.getChildAt(i); unbindVIEwReferences(vIEw); if (vIEw instanceof VIEwGroup) unbindVIEwGroupReferences((VIEwGroup) vIEw); } try { vIEwGroup.removeAllVIEws(); } catch (Throwable mayHappen) { // AdapterVIEws,ListVIEws and potentially other VIEwGroups don't // support the removeAllVIEws operation } } @SuppressWarnings("deprecation") private static voID unbindVIEwReferences(VIEw vIEw) { // set all Listeners to null (not every vIEw and not every API level // supports the methods) try { vIEw.setonClickListener(null); vIEw.setonCreateContextMenuListener(null); vIEw.setonFocuschangelistener(null); vIEw.setonKeyListener(null); vIEw.setonLongClickListener(null); vIEw.setonClickListener(null); } catch (Throwable mayHappen) { } // set background to null Drawable d = vIEw.getBackground(); if (d != null){ d.setCallback(null); } if (vIEw instanceof ImageVIEw) { ImageVIEw imageVIEw = (ImageVIEw) vIEw; d = imageVIEw.getDrawable(); if (d != null){ d.setCallback(null); } imageVIEw.setimageDrawable(null); imageVIEw.setBackgroundDrawable(null); } // destroy WebVIEw if (vIEw instanceof WebVIEw) { WebVIEw webvIEw = (WebVIEw) vIEw; webvIEw.stopLoading(); webvIEw.clearFormData(); webvIEw.cleardisappearingChildren(); webvIEw.setWebChromeClIEnt(null); webvIEw.setWebVIEwClIEnt(null); webvIEw.destroyDrawingCache(); webvIEw.destroy(); webvIEw = null; } if (vIEw instanceof ListVIEw) { ListVIEw ListVIEw = (ListVIEw) vIEw; try { ListVIEw.removeAllVIEwsInLayout(); } catch (Throwable mayHappen) { } ((ListVIEw) vIEw).destroyDrawingCache(); } } /** * exit System * @param context */ public voID exit(Context context) { exit(context,true); } /** * exit System * @param context * @param isClearCache */ @SuppressWarnings("deprecation") public voID exit(Context context,boolean isClearCache) { try { finishAllActivity(); if(context != null){ ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); activityMgr.restartPackage(context.getPackagename()); }// if(isClearCache){// LruCacheManager.getInstance().evictAll();// CacheManager.clearall();// } System.exit(0); androID.os.Process.killProcess(androID.os.Process.myPID()); } catch (Exception e) { e.printstacktrace(); } }}
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android编程之activity *** 作技巧总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android文件 *** 作技巧汇总》、《Android资源 *** 作技巧汇总》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
您可能感兴趣的文章:@H_419_15@浅谈Android系统的基本体系结构与内存管理优化Android中使用ContentProvider管理系统资源的实例Android开发之媒体播放工具类完整示例Android开发之d出软键盘工具类简单示例Android开发之EditText框输入清理工具类示例Android编程录音工具类RecorderUtil定义与用法示例Android开发中解析xml文件XmlUtils工具类与用法示例Android图片处理工具类BitmapUtilsAndroid封装的http请求实用工具类19个Android常用工具类汇总Android开发之超实用的系统管理工具类【SD卡,进程等】 总结以上是内存溢出为你收集整理的Android开发之Activity管理工具类完整示例全部内容,希望文章能够帮你解决Android开发之Activity管理工具类完整示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)