Prevent activity from being destroyed
Asked Answered
S

3

14

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.

Selfassertion answered 2/5, 2012 at 17:31 Comment(3)
how do you know its getting destroyed?Peshitta
I´ve put a Log in the OnDestroy() method, I tried a single app with only one activity and it works fine, but when I start using this in a more complex app the activity gets destroyed before the camera returns the imageSelfassertion
If you've found your own answer, post it and accept your own answer in order to close the question. Thank you.Outbreed
N
43

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

Nanji answered 17/11, 2013 at 22:55 Comment(7)
QA reported an issue - I could only reproduce it on the phone they use. I could not figure out why the activity ket exiting! Didn't know about this setting till I saw your comment. Thanks :)Giorgione
it was only on my tablet.. the issue...thanks bud +1Allodium
+100. Why is this even an option? It has soo many side-effects!Guenzi
Thank you very much. I have been struggling trying to find out why it is begin destroyed on my phone while in the emulator it is not.Loyalty
Some issues are so much haunting....There should be a movie on Developer code envy....Elam
This does not work as I don't have this option enabled either :(Copter
Lol what's the point of this option, I was going crazy trying to figure this outFerris
Y
4

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.

Yeta answered 2/5, 2012 at 17:46 Comment(8)
Only thing is that suddenly onActivityResult never is called, when I have an App with only one activity, the activity is never destroyed, but onActivityResult is called, now when inside another app suddenly the activity is destroyed and the result never seem to come back. Any idea how that can be?Selfassertion
Perhaps there is something wrong with your workflow. It seems odd that you are calling startActivityForResult() twice. You definitely need to call startActivityForResult() when launching the actual camera application, but I'm not so sure that you need it for the CameraHandler class. What is the CameraHandler class anyway? Is it possible to launch the camera app directly from the dashboard?Yeta
I need a screen in between, so directly from the dashboard is not an option, I found that startActivityForResult would prevent destroying the activity, but it does not, so indeed I might just as well remove that. The Camerahandler sends the intent to the camera and uploads a photo onactivity result, which it doesnt do now..Selfassertion
OK, so just to be clear... you have removed the startActivityForResult() call from your dashboard? (you might want to update your original post).Yeta
Have you taken a look at this? I can't tell you for sure what is wrong with your code based on the information you have given me, but I can tell you that something is definitely wrong if onActivityResult is never called when the Activity returns. Check out the sample code here and see if it helps.Yeta
thanks, hopefully I´ll find something. I posted the entire camerahandler class.Selfassertion
@AlexLockwood hello can you please tell me the right way to start activity startActivityForResult i m facing same issue just because of activity being killedAttune
"it won't change the expected behavior"- sure it can. if you keep state in your activity and don't save that state.Sumptuous
C
-3

You have to mention in your manifest as

 android:persistent="true"

Below SO posts answers the same question

How to make an activity stop, rather then be destroyed, from the BACK key?

How to prevent call of onDestroy() after onPause()?

Prevent activity from being destroyed as long as possible

Christean answered 4/3, 2016 at 7:16 Comment(3)
This attribute does not exist for activities: developer.android.com/guide/topics/manifest/…Priestess
@Priestess it does exists developer.android.com/guide/topics/manifest/…Christean
This is for the application, not the activity.Pictorial

© 2022 - 2024 — McMap. All rights reserved.