Skip to content

Commit

Permalink
修复可能的内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
goweii committed Nov 7, 2019
1 parent 20307f9 commit 7554503
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions anylayer/src/main/java/per/goweii/anylayer/ActivityHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import android.os.Bundle;
import android.text.TextUtils;

import java.util.Stack;
import java.util.LinkedList;
import java.util.List;

/**
* @author CuiZhen
Expand All @@ -19,16 +20,14 @@ final class ActivityHolder implements Application.ActivityLifecycleCallbacks {
private static ActivityHolder INSTANCE = null;

private final Application mApplication;
private final Stack<Activity> mActivityStack = new Stack<>();
private final List<Activity> mActivityStack = new LinkedList<>();

private ActivityHolder(Application application) {
Utils.requireNonNull(application, "application == null");
mApplication = application;
mApplication = Utils.requireNonNull(application, "application == null");
application.registerActivityLifecycleCallbacks(this);
}

static void init(Application application) {
Utils.requireNonNull(application, "application == null");
if (INSTANCE == null) {
INSTANCE = new ActivityHolder(application);
}
Expand All @@ -46,7 +45,7 @@ static Activity getActivity(Class<Activity> clazz) {
if (INSTANCE == null) {
return null;
}
if (INSTANCE.mActivityStack.empty()) {
if (INSTANCE.mActivityStack.isEmpty()) {
return null;
}
final int size = INSTANCE.mActivityStack.size();
Expand All @@ -63,17 +62,15 @@ static Activity getCurrentActivity() {
if (INSTANCE == null) {
return null;
}
if (INSTANCE.mActivityStack.empty()) {
if (INSTANCE.mActivityStack.isEmpty()) {
return null;
}
return INSTANCE.mActivityStack.peek();
return INSTANCE.mActivityStack.get(INSTANCE.mActivityStack.size() - 1);
}

// Application.ActivityLifecycleCallbacks

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
mActivityStack.push(activity);
mActivityStack.add(activity);
}

@Override
Expand All @@ -93,11 +90,11 @@ public void onActivityStopped(Activity activity) {
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
public void onActivityDestroyed(Activity activity) {
mActivityStack.remove(activity);
}

@Override
public void onActivityDestroyed(Activity activity) {
mActivityStack.pop();
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
}

0 comments on commit 7554503

Please sign in to comment.