title: How to limit the size of the back stack in Android?
body: <p>We are developing an Android app that contains very heavy views (it is used to present a company's catalog).</p>
  
  <p>The design of the app has some flaws, mainly because we were "forced" to mimic as much as possible the existing iOS version of the app (I know, this is wrong). Now we are facing some memory issues (=OOM exceptions), mainly because the navigation is not very structured and we cannot enforce a "finite" depth for the navigation: at the moment the user can make the back stack grow as much as he wants.</p>
  
  <p>Let me try to explain in a better way. In our app we have 4 sections <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code>. User can enter section <code>A</code>, open sub-section <code>A2</code> and then sub-sub section <code>A3</code>. He can do the same in <code>B</code>, with <code>B2</code> and <code>B3</code>. But he can also do something like this:</p>
  
  <pre><code>A -&gt; A2 -&gt; A3 -&gt; B2 -&gt; A3 -&gt; B2 -&gt; A3 -&gt; ... !OOM Exception!
  </code></pre>
  
  <p>Unfortunately, we are not allowed to change design of the app.</p>
  
  <p>How to prevent the app from creating many activities and having the stack size to grow too much? When the activity stops we do <em>as much cleanup as possible</em> (release resources, recycle bitmaps) but that just limits the size of the problem, it doesn't eliminate it.</p>
  
  <p>We are now thinking about marking all the activities as <strong>singleInstance</strong>. What do you think about this (yes, it is ugly)? Or can we somehow constrain the stack size in some other smarter way?</p>