Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent activity from being destroyed

I'm sending an intent to the camera from an activity that I call like this:

 Intent testphoto = new Intent(Dashboard.this,CameraHandler.class);
 startActivity(testphoto);

In the CameraHandler class I call the camera:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 0);

But before onActivityResult gets called in the CameraHandler class the activity is destroyed. Is there anyway to prevent this?

FOUND THE ANSWER: I had noHistory="true" in my AndroidManifest and that made the OS destroy the activity before the result.

like image 990
Diego Avatar asked May 02 '12 17:05

Diego


2 Answers

Be sure you don't have the "Don't keep activities" Developer setting on, as it will destroy the activity you are leaving.

like image 100
Omaraf Avatar answered Oct 06 '22 23:10

Omaraf


You don't have to worry about the calling Activity being destroyed when you call startActivityForResult(), as it won't change the expected behavior at all (i.e. the child activity will remember to pass the result back to the parent whether the parent is destroyed or not). See this post.

Also note that while it is sometimes necessary to prevent Activitys from being destroyed (i.e. on configuration changes, etc.), in general you want to leave the Activity lifecycle alone and let the system manage background Activitys for you. Once you launch a new Activity, you shouldn't be explicitly preventing previous Activitys from being destroyed.

like image 40
Alex Lockwood Avatar answered Oct 06 '22 21:10

Alex Lockwood