Skip to content

Commit

Permalink
增加对所有dialog引用的维护,并解决loadingdialog的内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Mar 24, 2018
1 parent b74548f commit 6ffcf0e
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.hss01248.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Build;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

/**
Expand All @@ -21,6 +24,7 @@ public class ActivityStackManager {
private static Stack<Activity> mActivityStack = new Stack<>();



private ActivityStackManager() {

}
Expand Down Expand Up @@ -193,6 +197,7 @@ public void addActivity(Activity activity) {
*/
public void removeActivity(Activity activity) {
mActivityStack.remove(activity);
DialogsMaintainer.onDestory(activity);
Log.e("dialog","mActivityStack.size()--removeActivity:"+activity.getClass().getSimpleName()+mActivityStack.size());
}

Expand Down
125 changes: 125 additions & 0 deletions dialog/src/main/java/com/hss01248/dialog/DialogsMaintainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.hss01248.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
* Created by hss on 2018/3/24.
*/

public class DialogsMaintainer {

private static HashMap<Activity,List<Dialog>> dialogsOfActivity = new HashMap<>();
private static List<Dialog> loadingDialogs = new ArrayList<>();



public static void addLoadingDialog(Dialog dialog){
if(dialog.getOwnerActivity() instanceof Activity){
loadingDialogs.add(dialog);
}

}

public static void dismissLoading(Activity activity){
if(activity ==null){
return;
}
ListIterator<Dialog> iterator = loadingDialogs.listIterator();
while (iterator.hasNext()){
Dialog dialog = iterator.next();
if(dialog.getOwnerActivity().equals(activity)){
dialog.dismiss();
iterator.remove();
removeWhenDismiss(dialog);
}
}
}

public static void addWhenShow(Dialog dialog){

Activity activity = dialog.getOwnerActivity();
if(activity ==null){
return;
}
List<Dialog> dialogs = null;
if(dialogsOfActivity.containsKey(activity)){
dialogs = dialogsOfActivity.get(activity);
}
if(dialogs == null){
dialogs = new ArrayList<>();
dialogsOfActivity.put(activity,dialogs);
}
dialogs.add(dialog);
}

public static void removeWhenDismiss(Dialog dialog){
Activity activity = dialog.getOwnerActivity();
if(activity ==null){
return;
}
if(!dialogsOfActivity.containsKey(activity)){
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
return;
}
ListIterator<Dialog> iterator = dialogInterfaces.listIterator();
while (iterator.hasNext()){
Dialog dialog0 = iterator.next();
if(dialog.equals(dialog0)){
iterator.remove();
}
}

}

/**
* 在这里移除已经dismiss的dialog引用
* @param activity
*/
public static void onPause(Activity activity){
if(!dialogsOfActivity.containsKey(activity)){
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
dialogInterfaces.remove(activity);
return;
}
ListIterator<Dialog> iterator = dialogInterfaces.listIterator();
while (iterator.hasNext()){
Dialog dialog = iterator.next();
if(!dialog.isShowing()){
iterator.remove();
}
}

}

public static void onDestory(Activity activity){
if(!dialogsOfActivity.containsKey(activity)){
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
dialogInterfaces.remove(activity);
return;
}
for (Dialog dialog : dialogInterfaces) {
if(dialog!=null){
dialog.dismiss();
}
}
dialogInterfaces.remove(activity);
}
}
Loading

0 comments on commit 6ffcf0e

Please sign in to comment.