欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之Activity管理工具類完整示例

 更新時間:2018年01月30日 09:25:32   作者:沉水之木  
這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Android開發(fā)之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();
    }
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息

    android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息

    這篇文章主要介紹了android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息,非常具有實用價值,需要的朋友可以參考下。
    2017-02-02
  • windows10安裝adb/fastboot驅(qū)動超詳細(xì)圖文教程

    windows10安裝adb/fastboot驅(qū)動超詳細(xì)圖文教程

    這篇文章主要介紹了windows10安裝adb/fastboot超詳細(xì)圖文教程,安裝方法也很簡單,只要adb安裝成功,fastboot就安裝好了,文中給大家介紹了問題分析及解決方法,需要的朋友可以參考下
    2023-01-01
  • android通過拼音搜索中文的功能實現(xiàn)代碼

    android通過拼音搜索中文的功能實現(xiàn)代碼

    這篇文章主要介紹了android通過拼音搜索中文的功能實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Android Init進(jìn)程對信號的處理流程詳細(xì)介紹

    Android Init進(jìn)程對信號的處理流程詳細(xì)介紹

    這篇文章主要介紹了Android Init進(jìn)程對信號的處理流程詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android中TextView和ImageView實現(xiàn)傾斜效果

    Android中TextView和ImageView實現(xiàn)傾斜效果

    這篇文章主要為大家詳細(xì)介紹了Android中TextView和ImageView實現(xiàn)傾斜效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android中AlarmManager基本用法分析

    Android中AlarmManager基本用法分析

    這篇文章主要介紹了Android中AlarmManager基本用法,結(jié)合實例形式簡單分析了AlarmManager的基本類型、方法及簡單使用示例,需要的朋友可以參考下
    2016-08-08
  • android打開應(yīng)用所在的市場頁面進(jìn)行評分操作的方法

    android打開應(yīng)用所在的市場頁面進(jìn)行評分操作的方法

    這篇文章主要介紹了android打開應(yīng)用所在的市場頁面進(jìn)行評分操作的方法,涉及Android操作市場頁面評分效果的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • Android實現(xiàn)商城購物車功能的實例代碼

    Android實現(xiàn)商城購物車功能的實例代碼

    最近公司項目做商城模塊,需要實現(xiàn)購物車功能,主要實現(xiàn)了單選、全選,金額合計,商品刪除,商品數(shù)量加減等功能,這篇文章主要介紹了Android實現(xiàn)商城購物車功能,需要的朋友可以參考下
    2019-06-06
  • Android開發(fā)之Location用法實例分析

    Android開發(fā)之Location用法實例分析

    這篇文章主要介紹了Android開發(fā)中Location用法,結(jié)合實例形式分析了Android使用location控件獲取經(jīng)緯度信息的相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android開發(fā)退出程序的方法匯總

    Android開發(fā)退出程序的方法匯總

    Android程序有很多Activity,比如說主窗口A,調(diào)用了子窗口B,子窗口B又調(diào)用子窗口C,back返回子窗口B后,在B中如何關(guān)閉整個Android應(yīng)用程序呢? 下面腳本之家小編就給大家介紹android開發(fā)退出程序的幾種方法,感興趣的朋友參考下吧
    2016-03-03

最新評論