App hangs on Camera.release() - java

I have an application that calls a custom activity within it for the purpose of recording audio and video. It uses the MediaRecorder and the Camera classes. My issue is that when the user hits the back button or cancel while the video is recording, the activity always hangs on Camera.release(), but if the stop button is pressed first, there's no problem. Here's my code:
Stop button code:
mRecorder.stop(); // Stop recording
mRecorder.reset(); // Reset recorder
camera.stopPreview();
onDestroy():
mRecorder.reset(); // Release media recorder
mRecorder.release();
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
}
mRecorder = null;
camera = null;
I tried adding mRecorder.stop() to onDestroy() as well but that didn't solve it. I checked here but none of the answers worked. I'm really stumped with this one.

Code such as this should be put in onPause() and not onDestroy() to be sure it gets called when needed.
According to the docs
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
You aren't saving data here but the same principle applies. So stopping the recording in onPause() will guarantee the code runs when the user presses the Back button.
Overriding onBackPressed() would work too but that wouldn't account for if the Activity went into the background for other reasons.

Related

How to completely destroy an activity from code?

I am building an medication reminder app in android using java. User can set one or more reminders and according to those will get notified to take the medicine.
Problem is, whenever a notification is generated, and the user taps on it and the receiver activity opens up, the user is presented with two choices of either taking the medicine or skipping it. Now, I have made sure that in both the cases, the activity will finish and have called its onDestroy() method too. My objective is to prevent the activity from appearing in the recent app list, so that the user can not repeatedly take or skip medicines.
Here's the details(the links all point to screenshots of the app if it helps in any way):
The notification comes,User taps on the notification and the Reminder Receive activity opens up
User either takes or skips the medicine, and the activity finishes.
but the activity is still being shown on the recent app list, and if tapped on it, it opens up the reminder receive activity again, with the user able to perform the same action again
I want to prevent this particular behaviour from happening.
Here's the things I have tried(I have a fragment,running on top of the activity):
finishing the activity from fragment,then calling onDestroy() on it
ReminderRecieveActivity activity = (ReminderRecieveActivity) requireActivity();
activity.finish();
activity.onDestroy();
Modifying the onDestroy() method of the activity like so
#Override
protected void onDestroy() {
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
System.exit(0);
}
But still the problem persists, Please help.
Why not create a separate Activity with android:noHistory="true" and also android:excludeFromRecents="true"? You don't have to kill the process to do that.

Android - calling finish() on activity does not stop it from running

So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.
So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.
To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)
Here's the link to the class on pastebin:
http://pastebin.com/V7z5c3HH
Thanks for your help! =D
Unsubscribe your location listener in the onDestroy method.
However, what you need for your GPS processing is probably a Service, not an Activity.
You need to remove the updates for that listener.
#Override
protected void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}

Is there a way to tell android force stop activity which going to background without removing it from activity stack?

I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.

The sound stopped when press lock button

I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.

Android Java: Understanding Activity Life?

I'm new to android development and I get how the android has an activity life.
If I have an app and I press a button to use the phone's camera funcationality like so...
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST);
}
How does onPause() or onDestroy() and the other stuff work?
I have this outside the onCreate()
protected void onPause(){
super.onPause();
}
If I want to press the back button or press the home button, do I have to destroy or pause the camera function? If so, I'm still trying to figure out how to do so?
Thanks!
When you start new activity from your current activity then there are two possibility of your current activity
Pause
Stop
Paused:
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped:
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
For example you are starting the Camera activity from your activity then your current activity will be Stop because the Camera activity will covers all your screen and your activity is not visible to camera activity.
Here is the complete description.
You are starting the Camera activity using the Intent so you do not have to handle the call back methods of the camera activity. System will manage the call back method you do not have to manage it.You just have to manage the Activity result which you will get in your activity from the Camera activity.
EDIT
And also ob course you never have to directly call any life cycle methods of the Activity.System automatically calls this method according to activity state.You just have to write your implementation in this methods to do your work.

Categories