0

I encountered this strange problem with my activity. I am sure the reason for this is documented somewhere but my search efforts have been in vain so-far.

To summarise the problem - my onCreate() is being called while the activity is in a paused state. According to all the life-cycle flow diagrams I have seen - this should never happen.

Here is (I think) the relevant info from my manifest:

   <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:mimeType="text/xml" />
            <data android:mimeType="application/xml" />
            <data android:pathPattern=".*\\.xml" />
        </intent-filter>
    </activity>

Let's say I start my application in the usual way by clicking on the icon. I then hit the home button. I can see that the OS calls onPause() - but not onDestroy() - which is what I expect.

If at this point I find an xml file and use my application to open it I see onCreate() being called - why does this happen?

I probably wouldn't even have noticed if it wasn't for the fact that my onCreate() initialises a rather large memory cache as a fragment and for this subsequent startup the findFragmentByTag returns null, even though the application that exists in the resumed state has already created this and I end up with an OutOfMemory exception.

Hopefully someone can shed a light on this.

Thanks,

Lew

sooper
  • 5,991
  • 6
  • 40
  • 65
Lieuwe
  • 1,734
  • 2
  • 27
  • 41

2 Answers2

0

Pressing the home button [usually] doesn't destroy an activity. It will remain in memory until the user returns, or the OS needs memory.

onCreate is called as your activity it being created for use, usually for the first time. However activities are destroyed and recreated when the device switches orientation. This is why onSaveInstanceState and onRestoreInstanceState exist. Creation is different from onStart or onResume. OnStart is more telling the activity that is is being started for the first time. OnResume is telling the Activity that it is now the focused (top most) Activity.

If at this point I find an xml file and use my application to open it I see onCreate() being called - why does this happen? I'm not sure what you mean by this? How are you using an XML file to "see" onCreate?

As for the rest of your question, I have a feeling that it resides in your onCreate, onStart or onPause methods. If you could post them, we could take a gander.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • As you can see I created an intent filter that will show my application as "option" to open an xml file (for instance when you download one in your browser). Problem I had is that when I do this - the old instance of my activity is still running but not used. – Lieuwe Dec 18 '12 at 15:55
  • @Lieuwe If that is the case, then I think your are only assigning data (received from the Intent) to your activity in onCreate. I should be done in onResume. – ahodder Dec 18 '12 at 16:13
0

Presumably your activity reads XMLs?

If that is so, what is happening is that a completely new activity is being created each time you open an XML. You can force the system to have only one instance of your Activity, but that means that whatever the user was looking at will be gone.

Here's the docs for that launchMode setting.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • I have now added android:launchMode="singleTop" to the manifest. At first this seems to have solved the problems I was seeing. When I now download an xml in my browser and then click on it it will send a new intent to my existing activity. However when I use the gmail app and I click on an xml attachment it seems to completely ignore this setting and it still starts a new activity. – Lieuwe Dec 18 '12 at 17:46