Skip to content

Commit 2b2563c

Browse files
committed
The beforeBindView () callback method has been added to CoreView; the register method of ViewModelEventBus has been modified, which makes it occupy less memory.
1 parent 968974d commit 2b2563c

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

mvvm/src/main/java/com/wutaodsg/mvvm/core/BaseMVVMActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public abstract class BaseMVVMActivity<VM extends BaseViewModel, DB extends View
3232
@Override
3333
protected void onCreate(@Nullable Bundle savedInstanceState) {
3434
super.onCreate(savedInstanceState);
35+
beforeBindView();
3536
mBaseViewProxy = new BaseViewProxy<>(this, this, newViewModel());
3637
}
3738

@@ -82,6 +83,12 @@ public VM newViewModel() {
8283
return null;
8384
}
8485

86+
@Override
87+
@CallSuper
88+
public void beforeBindView() {
89+
90+
}
91+
8592
@Override
8693
@CallSuper
8794
public void beforeDetach() {

mvvm/src/main/java/com/wutaodsg/mvvm/core/BaseMVVMFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public abstract class BaseMVVMFragment<VM extends BaseViewModel, DB extends View
4040
@Override
4141
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
4242
savedInstanceState) {
43+
beforeBindView();
4344
return inflater.inflate(getLayoutResId(), container, false);
4445
}
4546

@@ -97,6 +98,12 @@ public VM newViewModel() {
9798
return null;
9899
}
99100

101+
@Override
102+
@CallSuper
103+
public void beforeBindView() {
104+
105+
}
106+
100107
@Override
101108
@CallSuper
102109
public void beforeDetach() {

mvvm/src/main/java/com/wutaodsg/mvvm/core/CoreView.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ public interface CoreView<VM extends BaseViewModel, DB extends ViewDataBinding>
3232
VM newViewModel();
3333

3434
/**
35-
* 这个方法将在 View 被销毁时,解绑 ViewModel 和 DataBinding 之前被回调。
36-
* 因此这允许你在解绑之前做一些数据保存等的工作。
35+
* 这个方法将在 View 绑定 UI 视图(也就是 Layout XML 文件)之前调用。
36+
* 此时,ViewModel 和 DataBinding 也都还没有绑定。
37+
*/
38+
void beforeBindView();
39+
40+
/**
41+
* 这个方法将在 View 被销毁时(一般是 onDestroy() 方法被调用的时候),
42+
* 解绑 ViewModel 和 DataBinding 之前被回调。因此这允许你在解绑之前做一些数据保存等的工作。
3743
*/
3844
void beforeDetach();
3945

mvvm/src/main/java/com/wutaodsg/mvvm/util/vmeventbus/ViewModelEventBus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public <T> boolean register(@NonNull String eventTag,
104104
eventMap.put(dataClass, viewModelCommands);
105105
}
106106
} else {
107-
eventMap = new ConcurrentHashMap<>();
107+
eventMap = new ConcurrentHashMap<>(2);
108108
CopyOnWriteArrayList<ViewModelCommand> viewModelCommands = new CopyOnWriteArrayList<>();
109109
viewModelCommands.add(command);
110110
eventMap.put(dataClass, viewModelCommands);
@@ -148,7 +148,7 @@ public boolean register(@NonNull String eventTag,
148148
eventMap.put(NoDataEventType.class, viewModelCommands);
149149
}
150150
} else {
151-
eventMap = new ConcurrentHashMap<>();
151+
eventMap = new ConcurrentHashMap<>(2);
152152
CopyOnWriteArrayList<ViewModelCommand> viewModelCommands = new CopyOnWriteArrayList<>();
153153
viewModelCommands.add(command);
154154
eventMap.put(NoDataEventType.class, viewModelCommands);

0 commit comments

Comments
 (0)