1414
1515package apijson .demo .application ;
1616
17+ import android .app .Activity ;
1718import android .app .Application ;
19+ import android .os .Bundle ;
1820import android .util .Log ;
21+
22+ import java .lang .ref .WeakReference ;
23+ import java .util .LinkedList ;
24+ import java .util .List ;
25+
1926import apijson .demo .R ;
2027
2128/**Application
2431public class DemoApplication extends Application {
2532 private static final String TAG = "DemoApplication" ;
2633
27-
28- private static Application instance ;
29- public static Application getInstance () {
34+
35+ private static DemoApplication instance ;
36+ public static DemoApplication getInstance () {
3037 return instance ;
3138 }
32-
39+
3340 @ Override
3441 public void onCreate () {
3542 super .onCreate ();
3643 instance = this ;
3744 Log .d (TAG , "项目启动 >>>>>>>>>>>>>>>>>>>> \n \n " );
38-
45+
46+ registerActivityLifecycleCallbacks (new ActivityLifecycleCallbacks () {
47+
48+
49+ @ Override
50+ public void onActivityStarted (Activity activity ) {
51+ Log .v (TAG , "onActivityStarted activity = " + activity .getClass ().getName ());
52+ }
53+
54+ @ Override
55+ public void onActivityStopped (Activity activity ) {
56+ Log .v (TAG , "onActivityStopped activity = " + activity .getClass ().getName ());
57+ }
58+
59+ @ Override
60+ public void onActivitySaveInstanceState (Activity activity , Bundle outState ) {
61+ Log .v (TAG , "onActivitySaveInstanceState activity = " + activity .getClass ().getName ());
62+ }
63+
64+ @ Override
65+ public void onActivityResumed (Activity activity ) {
66+ Log .v (TAG , "onActivityResumed activity = " + activity .getClass ().getName ());
67+ setCurrentActivity (activity );
68+ }
69+
70+ @ Override
71+ public void onActivityPaused (Activity activity ) {
72+ Log .v (TAG , "onActivityPaused activity = " + activity .getClass ().getName ());
73+ setCurrentActivity (activityList .isEmpty () ? null : activityList .get (activityList .size () - 1 ));
74+ }
75+
76+ @ Override
77+ public void onActivityCreated (Activity activity , Bundle savedInstanceState ) {
78+ Log .v (TAG , "onActivityCreated activity = " + activity .getClass ().getName ());
79+ activityList .add (activity );
80+ }
81+
82+ @ Override
83+ public void onActivityDestroyed (Activity activity ) {
84+ Log .v (TAG , "onActivityDestroyed activity = " + activity .getClass ().getName ());
85+ activityList .remove (activity );
86+ }
87+
88+ });
3989 }
4090
4191
@@ -52,5 +102,20 @@ public String getAppVersion() {
52102 return getResources ().getString (R .string .app_version );
53103 }
54104
105+ private List <Activity > activityList = new LinkedList <>();
106+
107+ private WeakReference <Activity > sCurrentActivityWeakRef ;
108+ public Activity getCurrentActivity () {
109+ Activity currentActivity = null ;
110+ if (sCurrentActivityWeakRef != null ) {
111+ currentActivity = sCurrentActivityWeakRef .get ();
112+ }
113+ return currentActivity ;
114+ }
55115
116+ public void setCurrentActivity (Activity activity ) {
117+ if (sCurrentActivityWeakRef == null || !activity .equals (sCurrentActivityWeakRef .get ())) {
118+ sCurrentActivityWeakRef = new WeakReference <>(activity );
119+ }
120+ }
56121}
0 commit comments