Hell level 4 unleashed by Android Strict Mode! Dare you challenge it?

If you wonder why level 4… refer to my previous stories talking about level 1–3 below.

So what is this level 4… Let me tell the story. At the end, I’ll give you opportunity to challenge Hell 4. by providing you the code in github that replicates the issue. Let me know if you get through it :)

My Android Strict Mode is ON in Debug mode, with Death Penalty, VMPolicy detectAll. Refers to link above on how this is turned on.

How I created another activity for the App. In this activity, it’s functionality is for Login (so let me call in Login Activity). So it has 2 EditTexts (i.e. user name and password) and some Buttons. Simple.

What happens is, when I enter from Main Activity to Login Activity; Logout back to Main Activity, and again to Login Activity (i.e. testing login, logout, login scenario etc). The App then crash with the following issue.

E/StrictMode: class com.my.LoginActivity; instances=2; limit=1
android.os.StrictMode$InstanceCountViolation: class com.my.LoginActivity; instances=2; limit=1
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)

Clearly my LoginActivity has leaked!! So let’s start the debugging process…

Refer to the below excellent tutorial of how to debug memory leak, I came to no conclusion.

Just to clarify, the above indeed is a great tutorial and could identify the source of leak… If the leak is from your code :)

Clearly, this is not something from my code at all. I use the most native approach of debugging… Striptease Debugging. It’s simply slowly rip off one part code at a time, and see if the problem still occurs.

It takes a while, as until my almost entire Kotlin code gone with only the view inflated, the issue still persist. So, now I get to strip my layout.xml as well…. an Viola!! … The Culprit is EditText!!!

Simply removing EditText, the issue is gone. Put it in, the memory leaks occurs. But this is a Login Page, I can’t live without EditText…. So let me search if there’s such issue out there…

Perform this Google Search https://www.google.com.au/search?q=EditText+Leak… You’ll find tans of them… Just list few that is StackOverflow reported below.

From there, various solution given. They are as below

  • Don’t use RelativeLayout
  • Use Edit Test hint
  • Don’t set wrap_content on the width
  • Disable the blinking cursor
  • Don’t set onTextChange listener
  • Set textNoSuggestions
  • … many more, with some extends something from EditText, and some using Refactor…

All doesn’t work on my side, except one…

Replace the activity context with application context when inflating the layout (I’m inflating in my Fragment, using Kotlint). i.e. instead of

override fun onCreateView(inflater: LayoutInflater, 
container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate
(R.layout.fragment_login, container, false)


using the below

override fun onCreateView(inflater: LayoutInflater, 
container: ViewGroup?, savedInstanceState: Bundle?): View? {
return LayoutInflater.from(activity.applicationContext)
.inflate(R.layout.fragment_sign_in, container, false)

But this is not good, as using explains in http://stackoverflow.com/a/19428585 due to Theme related issue. For me, it crashes on other things when I “redress” my code (as they have previous been stripped ;P)

Well, deep dive of code is not helping, so now the question is, since this seemingly an Android issue, does it happens across all Android version?

This thought came about when I see this Code Change

Perhaps it has been fixed on the latest version. On device I tested on KitKat (Samsung Note4), Lollipop (Samsung S5), Marshmallow (Samsung S7) and Nougat (Nexus 6p). The issues happens only on KitKat and Lollipop.

But the fact that it happens on Lollipop, worries me. So I tested using emulator on SDK version 19 and 21. Version 19 exhibits the issue, but mot version 21, which is Lollipop as well.

So I am confuse, does Lollipop has the issue or not. The Samsung S5 Lollipop I use is version 5.0, and the emulated Nexus 5 Lollipop is version 5.0.2. Perhaps the issue happen on 5.0 and not after? I don’t know.

My next plan, is to turn off memory leak detection for all Lollipop and below. And write Stackoverflow, with with code, to see if there’s any good soul out there to help me out of Hell 4 ;P

The Code is at

The stackoverflow is at

If you have dare to take the challenge visiting Hell 4, explore and let me know your finding (what caused the leak, and how it could be solved)… Thanks in advanced. All the best!

Written by

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store