5

I am launching a media intent to take a photo. After the user took a picture and gets back to the previous activity the whole application has restarted.

As this doesn't happen all the time, I think my application goes to the background and android kills it when the device has low memory.

Is there any way to keep my application from going to the background?

Thanks!

timoschloesser
  • 2,790
  • 4
  • 25
  • 33
  • you can't stop your application from going to background but you can probably make it save it's state and restore that – zapl Apr 16 '12 at 14:58
  • If you want to take a photo, your app has to go to the background..behind the camera app.. – Ron Apr 22 '12 at 06:36
  • Show the code that you use for launching the camera.. – Ron Apr 22 '12 at 06:36
  • May be there's something after the camera launch code thats causing the problem. – Ron Apr 22 '12 at 06:41

5 Answers5

13

This is normal behavior in Android, all activities currently not visible on screen (after onStop) can be killed without notice (i.e. onDestory) if the system has low memory.

This usually happens to our app on one of our test devices which has memory issues regardless of our app.

You can usually recreate this behavior when you open the camera via the intent, and then rotate the device (portrait to landscape), this will also kill and re-create your app.

To solve this you need to extend onSaveInstanceState and use the savedInstanceState parameter in your onCreate to pass from your killed instance to your new instance some important information (like "we're in the middle of getting a pic from the camera").

marmor
  • 27,641
  • 11
  • 107
  • 150
2

I can think of two possibilities...

  1. It's not killing the activity, but the intent launches a new activity. You can stop this by putting a tag in your manifest.xml as an attribute in the activity tag like this:

    <activity android:name=".nameOfActivity"
    android:launchMode="singleTop" />

  2. Make sure that the media intent is under the activity to handle the photo, and not a main/launcher activity.

Drake Clarris
  • 1,047
  • 6
  • 10
1

IMO two options:

  1. Implement onSaveInstanceState/onRestoreInstanceState or
  2. Make your activity a service.
1

I faced this issue and got a solution after R&D for it. Set Target Android 4.0 and then add this line in AndroidManifest.xml of activity:

android:configChanges="screenLayout|uiMode|orientation|screenSize|smallestScreenSize"

It works for me.

raisam
  • 11
  • 1
0

When you start media intent use following method instead of startActivity(intent)

startActivityForResult(intent, REQUEST_CODE); //private int REQUEST_CODE = 232

When the started activity finishes, your calling activity will be started. You need to handle this using following function

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
      //Perform your task       
      }
}

The activity started need to override follwoing method

@Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(REQUEST_CODE, intent);
        super.onBackPressed();
    }

startActivityForResult() is an special way for starting activity for a specific task and get the desired result. The called activity will send the data back.

You can use the method putExtra() & getExtra in intent to send and receive data between two activity.

This will solve your problem hopefully. If you have doubts comment. And if possible share your code so it can be more clear.

Raj Shah
  • 322
  • 1
  • 22