0

I have an object sitting in memory on the application that I'm using, and on a button press I do a startActivityForResult and launch the camera application, so I can attach a photo to that object. On every phone/tablet I've ever tested with (somewhere around 15 or so) it works completely fine, but for some reason with the Motorola Droid 3 (CDMA version) once the camera application starts, it's like onDestroy is called... even though it returns to my app after the photograph is snapped, all of the variables held in memory are erased. Can someone direct me as to how I can fix this please?

RyanInBinary
  • 1,533
  • 3
  • 19
  • 47
  • what happens when you get back to your activity, are you getting an `onCreate`? do you implement `onSaveInstanceState`? – dldnh Mar 26 '12 at 00:49

1 Answers1

1

I'm guessing what's happening is that the camera app uses a big enough chunk of memory that android needs to destroy the paused activity. If you look at this page,

http://developer.android.com/reference/android/app/Activity.html

It shows that possibility pretty clearly.

You are seeing differing behavior on different devices because different devices have different apps loaded into memory and different amounts of memory to begin with.

If you need to save state in your app, you can hook in at onSaveInstanceState() and onRestoreInstanceState(). Here's a post that talks about it in more detail.

How to save an activity state using save instance state?

In summary, don't depend on the state being the same when you resume an activity. If you depend on that happening, you need to handle it yourself.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • 1
    I agree with Jeffrey, you need to be prepared for your activity to be killed whenever it goes to the background. You can also use `onRetainNonConfigurationInstance` if the variable you're trying to keep is not easily parcelable. – dmon Mar 26 '12 at 01:07