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

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();
}

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.

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 get if minimized

Is there anyway to check if the application is minimized or if you have locked your device?
Because when I do minimize / lock my device the application is still runnig, this is mainly because I'd like to pause the music / sfx not to annoy people.. Like if someone is calling.
I am using Activity and SurfaceView with threads.
I have tried putting my pause method in the surfaceDestroyed / surfaceChanged but without success.
You should understand the activity lifecycle first
when the activity comes int the foreground it'll enter onPause() and it'll enter onResume if the user returns to the activity, an example to use onPause is like this
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// DO YOUR STUFF HERE
}
}
for furthere reference about onPause you can see it here -> onPause Tutorial

android - How to detect application being activated

When the app is launched, Application onCreate is called. How to detect when the app is brought to front from running in background?
Look for onResume() method. Its is always called when your app comes foreground.
As per google docs:
The foreground lifetime of an activity happens between a call to
onResume() until a corresponding call to onPause(). During this time
the activity is in front of all other activities and interacting with
the user. An activity can frequently go between the resumed and paused
states -- for example when the device goes to sleep, when an activity
result is delivered, when a new intent is delivered -- so the code in
these methods should be fairly lightweight.
CODE SAMPLE:
#Override
public void onResume()
{
super.onResume();
Log.d("tag", "This screen is back");
}
You can override onResume().
#Override
public void onResume()
{
Log.d("tag", "This screen is back");
}
However, I would agree with the comment that you probably should look more into this to see how Android works.

Categories