Android開發(fā)之Activity管理工具類完整示例
本文實(shí)例講述了Android開發(fā)之Activity管理工具類。分享給大家供大家參考,具體如下:
這個(gè)工具類是對(duì)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é)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android使用分類型RecyclerView仿各大商城首頁
這篇文章主要為大家詳細(xì)介紹了Android使用分類型的RecyclerView仿各大商城首頁,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
輕松實(shí)現(xiàn)Android3D效果通俗易懂
前幾天有粉絲要求計(jì)蒙寫一個(gè)3d效果的簡(jiǎn)單教程,其實(shí)這個(gè)在Android官方demo中是有的,可能對(duì)于新手而言看不太明白,于是根據(jù)本人自己的理解來寫一個(gè)教程,并改成粉絲要求的樣子2021-08-08
淺析Android手機(jī)衛(wèi)士手機(jī)定位的原理
手機(jī)定位的三種方式:網(wǎng)絡(luò)定位,基站定位,GPS定位。本文給大家介紹Android手機(jī)衛(wèi)士手機(jī)定位的原理,感興趣的朋友一起學(xué)習(xí)吧2016-04-04
淺談android Fragment橫豎屏翻轉(zhuǎn)對(duì)重新加載的要求
下面小編就為大家分享一篇淺談android Fragment橫豎屏翻轉(zhuǎn)對(duì)重新加載的要求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,支持多種機(jī)型
這篇文章主要介紹了android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01
Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android中URLEncoder空格被轉(zhuǎn)碼為"+"號(hào)的處理辦法
當(dāng)上傳文件的文件名中間有空格,用URLEncoder.encode方法會(huì)把空格變成加號(hào)(+)在前臺(tái)頁面顯示的時(shí)候會(huì)多出加號(hào),下面這篇文章主要給大家介紹了關(guān)于Android中URLEncoder空格被轉(zhuǎn)碼為"+"號(hào)的處理辦法,需要的朋友可以參考下2023-01-01
Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

