I have a code with Android Strict Mode turned on, VmPolicy detectAll and Penalty Death. This is turned on in Application.
So it helps me to detect memory leak. My code is very simple, 2 activities (almost blank). The MainActivity have a button click to open SubActivity. The SubActivity just have one EditText.
The code could be obtained here https://github.com/elye/issue_edittextleak
If you run the code, from MainActivity go to SubActivity, and return to MainActivity (using Back Key), and go to SubActivity, and return... It'll crash with
E/StrictMode: class com.elyeproj.edittextleak.SubActivity; instances=2; limit=1
android.os.StrictMode$InstanceCountViolation: class com.elyeproj.edittextleak.SubActivity; instances=2; limit=1
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
This is because it detected SubActivity has leaked.
Why SubActivity leaked, because it has EditText. Removing it will cause it to not leak anymore.
This happens on Samsung S5 Lollipop 5.0 (v21). It also leaks on KitKat (v19). It doesn't happens on Samsung S7 Marshmallow and Nexus 6 (Nougat).
It doesn't happens on Emulated Nexus 5 Lollipop 5.0.2 (v21). But happens on v19 of Emulated Nexus 5.
I have check on many stackoverflow, and can't find the solution. You'll read of more detail of this issue and my exploration in https://medium.com/@elye.project/hell-level-4-unleashed-by-android-strict-mode-dare-you-challenge-it-1dc9048bb4fb#.aiffbdikn
So what I want? I think the memory leak has been resolved on Lollipop 5.0.2 and beyond. But for version before that, how could I prevent the leak while I could have the editText?