Skip to content

Commit a132c15

Browse files
Nikolay BelikovWonderCsabo
authored andcommitted
Clean refs to injected views in onDestroyView()
1 parent 77259b6 commit a132c15

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ViewByIdHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public void process(Element element, EComponentWithViewSupportHolder holder) {
7373
JClass viewClass = codeModelHelper.typeMirrorToJClass(uiFieldTypeMirror, holder);
7474
JFieldRef fieldRef = ref(fieldName);
7575

76-
holder.assignFindViewById(idRef, viewClass, fieldRef);
76+
holder.processViewById(idRef, viewClass, fieldRef);
7777
}
7878
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public JInvocation findViewById(JFieldRef idRef) {
104104
return findViewById;
105105
}
106106

107+
public void processViewById(JFieldRef idRef, JClass viewClass, JFieldRef fieldRef) {
108+
assignFindViewById(idRef, viewClass, fieldRef);
109+
}
110+
107111
public void assignFindViewById(JFieldRef idRef, JClass viewClass, JFieldRef fieldRef) {
108112
String idRefString = codeModelHelper.getIdStringFromIdFieldRef(idRef);
109113
FoundViewHolder foundViewHolder = (FoundViewHolder) foundHolders.get(idRefString);

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class EFragmentHolder extends EComponentWithViewSupportHolder implements
8080
private JBlock onPauseBeforeSuperBlock;
8181
private JBlock onAttachAfterSuperBlock;
8282
private JBlock onDetachBeforeSuperBlock;
83+
private JBlock onDestroyViewAfterSuperBlock;
8384

8485
public EFragmentHolder(ProcessHolder processHolder, TypeElement annotatedElement) throws Exception {
8586
super(processHolder, annotatedElement);
@@ -216,13 +217,17 @@ protected void setInit() {
216217

217218
public JFieldVar getContentView() {
218219
if (contentView == null) {
219-
setContentView();
220-
setOnCreateView();
221-
setOnDestroyView();
220+
setContentViewRelatedMethods();
222221
}
223222
return contentView;
224223
}
225224

225+
private void setContentViewRelatedMethods() {
226+
setContentView();
227+
setOnCreateView();
228+
setOnDestroyView();
229+
}
230+
226231
private void setContentView() {
227232
contentView = generatedClass.field(PRIVATE, classes().VIEW, "contentView" + generationSuffix());
228233
}
@@ -253,8 +258,27 @@ private void setOnDestroyView() {
253258
JMethod onDestroyView = generatedClass.method(PUBLIC, codeModel().VOID, "onDestroyView");
254259
onDestroyView.annotate(Override.class);
255260
JBlock body = onDestroyView.body();
256-
body.assign(contentView, _null());
257261
body.invoke(_super(), onDestroyView);
262+
body.assign(contentView, _null());
263+
onDestroyViewAfterSuperBlock = body.block();
264+
}
265+
266+
private JBlock getOnDestroyViewAfterSuperBlock() {
267+
if (onDestroyViewAfterSuperBlock == null) {
268+
setContentViewRelatedMethods();
269+
}
270+
return onDestroyViewAfterSuperBlock;
271+
}
272+
273+
@Override
274+
public void processViewById(JFieldRef idRef, JClass viewClass, JFieldRef fieldRef) {
275+
super.processViewById(idRef, viewClass, fieldRef);
276+
clearInjectedView(fieldRef);
277+
}
278+
279+
private void clearInjectedView(JFieldRef fieldRef) {
280+
JBlock block = getOnDestroyViewAfterSuperBlock();
281+
block.assign(fieldRef, _null());
258282
}
259283

260284
private void setOnStart() {

0 commit comments

Comments
 (0)