Skip to content

Commit c1c6a60

Browse files
author
Nikolay Belikov
committed
Clean contentView_ reference in onDestroyView()
If setRetainInstance(true) has been set on the fragment or addToBackStack() was applied to FragmentTransaction, the fragment won't be destroyed and will keep the reference to its parent view in contentView_ and could lead to memory leak. This change implements onDestroyView() which cleans the reference. However, the developer is still responsible for cleaning up the injected views, because otherwise a (badly implemented) background task may get a NullPointerException.
1 parent 9713b64 commit c1c6a60

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

  • AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public JFieldVar getContentView() {
219219
if (contentView == null) {
220220
setContentView();
221221
setOnCreateView();
222+
setOnDestroyView();
222223
}
223224
return contentView;
224225
}
@@ -244,6 +245,14 @@ private void setOnCreateView() {
244245
body._return(contentView);
245246
}
246247

248+
private void setOnDestroyView() {
249+
JMethod onDestroyView = generatedClass.method(PUBLIC, codeModel().VOID, "onDestroyView");
250+
onDestroyView.annotate(Override.class);
251+
JBlock body = onDestroyView.body();
252+
body.assign(contentView, _null());
253+
body.invoke(_super(), onDestroyView);
254+
}
255+
247256
private void setOnStart() {
248257
JMethod onStart = generatedClass.method(PUBLIC, codeModel().VOID, "onStart");
249258
onStart.annotate(Override.class);

0 commit comments

Comments
 (0)