大佬教程收集整理的这篇文章主要介绍了Android开发之Activity管理工具类完整示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了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 Sy@L_618_2@ * @param context */ public void exit(Context context) { exit(context,truE); } /** * exit Sy@L_618_2@ * @param context * @param isClearCache */ @SuppressWarnings("deprecation") public void exit(Context context,Boolean isClearCachE) { try { finishAllActivity(); if(context != null){ Activitymanager activitymgr = (Activitymanager) context.getSy@L_618_2@service(Context.ACTIVITY_serviCE); activitymgr.restartPackage(context.getPackagename()); } // if(isClearCachE){ // LruCacheManager.geTinstance().evictAll(); // CacheManager.clearAll(); // } Sy@L_618_2@.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程序设计有所帮助。
以上是大佬教程为你收集整理的Android开发之Activity管理工具类完整示例全部内容,希望文章能够帮你解决Android开发之Activity管理工具类完整示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。