tags:

views:

44

answers:

1

I understand that the system may kill the activity without calling onDestroy(). Say I have a MediaPlayer that runs even when it's not visible, I would be releasing the player in onDestroy() method.

Now, if the system decides to kill the activity after calling onPause() and never called onDestroy(), what does it mean for my MediaPlayer? Is it ever released? This is bigger problem for Camera since I think it requires to be released for other activities to use it.

A: 

It is very very rare that you should keep media playing from an activity after onPause(). You most certainly should release it as it will create leaks. Instead your media should be playing in a Service that doesn't have the onPause() issues (a service is either running or not) and then your activity interfaces with the service.

Moncader
Yes using a service makes sense! but I was just trying to simplify it using a mediaplayer though.(I shouldn't have i guess)A better scenario is when Camera is opened for MediaRecorder preview in the main video activity and if you want another see-through activity on top of it with the camera preview still visible below.I believe the framework will only call onPause() then onStop() on the main activity. So if the framework now decides to kill the main activity and skip onDestroy(), camera is never released - is this right? Is there any workaround to guarantee camera.release()?
dzeikei
You are right. The camera must be released on onPause(). There is a reason. If you 'control' what activity is displayed on top of your preview. You can not control when a service or even more so, the system calls up another activity due to a system broadcast that needs the camera or music service. In that situation, the system gets very angry at you for not releasing the camera :)You should avoid displaying another activity over top a camera preview. Instead use getWindowManager().addView() to add what 'looks' like a new activity on top.
Moncader
Thanks! It was bugging me all day :)
dzeikei