Skip to content

Commit

Permalink
添加获取顶部Activity方法
Browse files Browse the repository at this point in the history
  • Loading branch information
beaven11 committed Apr 18, 2018
1 parent 50d9c40 commit 17747cc
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions common/src/main/java/conm/zhuazhu/common/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,47 @@ public class Utils {
@SuppressLint("StaticFieldLeak")
private static Application sApplication;

static WeakReference<Activity> sTopActivityWeakRef;
private static WeakReference<Activity> sTopActivityWeakRef;
static List<Activity> sActivityList = new LinkedList<>();

private static Application.ActivityLifecycleCallbacks mCallbacks = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
sActivityList.add(activity);
setTopActivityWeakRef(activity);
}
private static Application.ActivityLifecycleCallbacks mCallbacks =
new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
sActivityList.add(activity);
setTopActivityWeakRef(activity);
}

@Override
public void onActivityStarted(Activity activity) {
setTopActivityWeakRef(activity);
}
@Override
public void onActivityStarted(Activity activity) {
setTopActivityWeakRef(activity);
}

@Override
public void onActivityResumed(Activity activity) {
setTopActivityWeakRef(activity);
}
@Override
public void onActivityResumed(Activity activity) {
setTopActivityWeakRef(activity);
}

@Override
public void onActivityPaused(Activity activity) {
@Override
public void onActivityPaused(Activity activity) {

}
}

@Override
public void onActivityStopped(Activity activity) {
@Override
public void onActivityStopped(Activity activity) {

}
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

}
}

@Override
public void onActivityDestroyed(Activity activity) {
sActivityList.remove(activity);
}
};
@Override
public void onActivityDestroyed(Activity activity) {
sActivityList.remove(activity);
}
};

private Utils() {
throw new UnsupportedOperationException("u can't instantiate me...");
Expand Down Expand Up @@ -94,4 +95,7 @@ private static void setTopActivityWeakRef(final Activity activity) {
}
}

public static Activity getTopActivity() {
return sTopActivityWeakRef == null ? null : sTopActivityWeakRef.get();
}
}

0 comments on commit 17747cc

Please sign in to comment.