|
@@ -5,15 +5,17 @@ import android.support.annotation.Nullable;
|
5
|
5
|
import android.view.View;
|
6
|
6
|
import android.view.ViewTreeObserver;
|
7
|
7
|
|
|
8
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
9
|
+
|
8
|
10
|
public abstract class ViewController {
|
9
|
11
|
public interface LifecycleListener {
|
10
|
|
- void onCreate(ViewController viewController);
|
|
12
|
+ void onCreate();
|
11
|
13
|
|
12
|
|
- void onStart(ViewController viewController);
|
|
14
|
+ void onStart();
|
13
|
15
|
|
14
|
|
- void onStop(ViewController viewController);
|
|
16
|
+ void onStop();
|
15
|
17
|
|
16
|
|
- void onDestroy(ViewController viewController);
|
|
18
|
+ void onDestroy();
|
17
|
19
|
}
|
18
|
20
|
|
19
|
21
|
private enum LifecycleState {
|
|
@@ -23,14 +25,14 @@ public abstract class ViewController {
|
23
|
25
|
private final Activity activity;
|
24
|
26
|
private View view;
|
25
|
27
|
private StackController stackController;
|
26
|
|
- private LifecycleState lifecycleState;
|
|
28
|
+ private AtomicReference<LifecycleState> lifecycleState = new AtomicReference<>(LifecycleState.Destroyed);
|
27
|
29
|
private LifecycleListener lifecycleListener;
|
28
|
30
|
|
29
|
31
|
public ViewController(Activity activity) {
|
30
|
32
|
this.activity = activity;
|
31
|
33
|
}
|
32
|
34
|
|
33
|
|
- protected abstract View onCreateView();
|
|
35
|
+ protected abstract View createView();
|
34
|
36
|
|
35
|
37
|
public boolean handleBack() {
|
36
|
38
|
return false;
|
|
@@ -52,6 +54,7 @@ public abstract class ViewController {
|
52
|
54
|
public View getView() {
|
53
|
55
|
if (view == null) {
|
54
|
56
|
view = createView();
|
|
57
|
+ attachLifecycle();
|
55
|
58
|
}
|
56
|
59
|
return view;
|
57
|
60
|
}
|
|
@@ -60,24 +63,27 @@ public abstract class ViewController {
|
60
|
63
|
this.lifecycleListener = lifecycleListener;
|
61
|
64
|
}
|
62
|
65
|
|
63
|
|
- private View createView() {
|
64
|
|
- View view = onCreateView();
|
65
|
|
- lifecycleState = LifecycleState.Created;
|
66
|
|
- view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
|
|
66
|
+ private void attachLifecycle() {
|
|
67
|
+ view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
67
|
68
|
@Override
|
68
|
|
- public boolean onPreDraw() {
|
69
|
|
- if (lifecycleListener != null) {
|
70
|
|
- if (lifecycleState != LifecycleState.Started) {
|
71
|
|
- lifecycleState = LifecycleState.Started;
|
72
|
|
- lifecycleListener.onStart(ViewController.this);
|
|
69
|
+ public void onGlobalLayout() {
|
|
70
|
+ if (lifecycleListener == null) {
|
|
71
|
+ return;
|
|
72
|
+ }
|
|
73
|
+ if (view.getVisibility() == View.VISIBLE) {
|
|
74
|
+ if (lifecycleState.compareAndSet(LifecycleState.Created, LifecycleState.Started)) {
|
|
75
|
+ lifecycleListener.onStart();
|
|
76
|
+ }
|
|
77
|
+ } else {
|
|
78
|
+ if (lifecycleState.compareAndSet(LifecycleState.Started, LifecycleState.Stopped)) {
|
|
79
|
+ lifecycleListener.onStop();
|
73
|
80
|
}
|
74
|
81
|
}
|
75
|
|
- return true;
|
76
|
82
|
}
|
77
|
83
|
});
|
|
84
|
+ lifecycleState.set(LifecycleState.Created);
|
78
|
85
|
if (lifecycleListener != null) {
|
79
|
|
- lifecycleListener.onCreate(this);
|
|
86
|
+ lifecycleListener.onCreate();
|
80
|
87
|
}
|
81
|
|
- return view;
|
82
|
88
|
}
|
83
|
89
|
}
|