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):
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.