4

What I want to achieve:

The MainActivity should be reused, in other words, I don't want to run onCreate() over again, but only onResume() on the same Activity, but I have a strange behaviour. It seems that the launchMode in AndroidManifest is ignored. Whatever I set (singleTask, singleInstance), it always creates a new Activity which leads to growing heap. When I exit the app with the home button and start it again, every time a new instance is created. (observed that in .hprof leak report).

This is how it looks:

<activity
    android:name="com.mydomain.myapp.pro.MainActivity"
    android:launchMode="singleTask"
    android:screenOrientation="nosensor" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

As I tried to isolate the problem, I saw that the Activity will be killed on pressing home (onDestroy() is executed), but the leak reports shows this. Every time I launch the app, I get more and more MainActivity instances although the last instance is reputed to be destroyed. How can it be that onDestroy() was executed, but there are as many instances as much I started the Activity (directly by app start):

Leak Report

It's also interesting that onDestroy() is executed at all, because I never call finish() and I still have lots of resources. So there is no need for Android to kill the Activity, but the upper problem is more important at first. Maybe it is because I kill all fragments by finish() in MainActivity's on onPause(). Otherwise the fragments would leak. And the fragment's onDestroy() calls super.onDestroy() which is probably the MainActivity.

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • looks fine should be a problem within your activity code.. – A Honey Bustard Feb 22 '15 at 14:41
  • That's strange, because the only leak that is reported is the Activity itself. – Bevor Feb 22 '15 at 14:45
  • @Bevor Google doesn't recommend your approach OOTB. What are you trying to accomplish? The framework will handle memory concerns... – Aaron McIver Feb 22 '15 at 15:24
  • @AaronMcIver The problem is that none of the expected behaviours seem to work: In my opinion it's the default use case is when the user puts the Activity into the background (Home pressed, onPause()) and resumes it (onResume()). The Activity won't be killed unless Android decide it to do so. So that's the default behaviour I want to achieve. But even the default behaviour doesn't seems to work. Every time I bring back the Activity, onCreate() is executed and even on new instances. This seems to be completely wrong to me, especially when I set `singleInstance`. – Bevor Feb 22 '15 at 15:35

1 Answers1

1

I was able to isolate the problem. The multiple instances occur due to a fragment containing my AdMob implementation. It's no "Android" problem. The fragment somehow leaks. The code from above seems to be correct.

Bevor
  • 8,396
  • 15
  • 77
  • 141