1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.base;
20
21 import java.lang.ref.WeakReference;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24
25 import org.exoplatform.utils.Log;
26
27 import android.support.v4.app.FragmentActivity;
28
29
30
31
32
33 public abstract class BaseActivity extends FragmentActivity {
34
35
36
37
38
39
40 public interface ActivityLifecycleCallbacks {
41 void onResume(BaseActivity act);
42
43 void onPause(BaseActivity act);
44 }
45
46 public static class BasicActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
47
48 @Override
49 public void onResume(BaseActivity act) {
50
51 }
52
53 @Override
54 public void onPause(BaseActivity act) {
55
56 }
57
58 }
59
60 private HashSet<WeakReference<BasicActivityLifecycleCallbacks>> mLifeCycleCallbackSet =
61 new HashSet<WeakReference<BasicActivityLifecycleCallbacks>>();
62
63 public void addLifeCycleObserverRef(BasicActivityLifecycleCallbacks cbRef) {
64 mLifeCycleCallbackSet.add(new WeakReference<BaseActivity.BasicActivityLifecycleCallbacks>(cbRef));
65 }
66
67 public void removeLifeCycleObserver(BasicActivityLifecycleCallbacks removeCb) {
68 synchronized (mLifeCycleCallbackSet) {
69 ArrayList<WeakReference<BasicActivityLifecycleCallbacks>> removeList =
70 new ArrayList<WeakReference<BasicActivityLifecycleCallbacks>>();
71 for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
72 BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
73 if (cb == removeCb) {
74 removeList.add(cbRef);
75 }
76 }
77 for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : removeList) {
78 mLifeCycleCallbackSet.remove(cbRef);
79 }
80 }
81 }
82
83 @Override
84 protected void onResume() {
85 super.onResume();
86 for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
87 BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
88 if (cb != null) {
89 cb.onResume(this);
90 }
91 }
92 }
93
94 @Override
95 protected void onPause() {
96 for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
97 BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
98 if (cb != null) {
99 cb.onPause(this);
100 }
101 }
102
103 removeLifeCycleObserver(null);
104 if (Log.LOGD)
105 Log.d(getClass().getSimpleName(), "onPause isFinishing=", isFinishing(), " ", this);
106 super.onPause();
107 }
108 }