I have an app with one activity that I can't seem to stop. I have a button on it that executes finish(). Whether I use that or I use the back button on the phone the app continues to run.
I see it's still running by tapping on it in Applications and the Force Stop button is enabled.
What might be causing this?
Thanks, Gary
Suicide (Option 1)
Call finish(); on button click
Add this line to onDestroy method:
android.os.Process.killProcess(android.os.Process.myPid());
public void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
Suicide (Option 2)
Call this in the button listener
System.exit (0);
Try this code to stop your activity/app instead of finish(),
android.os.Process.killProcess(android.os.Process.myPid());
Related
In splash activity I want to exit app when calling onStop() (I want to stop app if user clicks home button , Close screen or press backbutton)
Unfortunately calling finish() didn't help , even after exiting app the splash activity continue working and even starts the next activity
I would like to mention that there is an asynctask class inside splash.activity and there is also another class jsonfetch called in splash.activity ( fetch data from server and open next activity after ) I think this one doesnt stop because when exiting app it opens the next activity after few secondes
I suspect you are navigating to your MainActivity from you SplashActivity.
Try adding a global variable shouldNavigate to your SplashActivity and change it to false if onStop() is called;
private boolean shouldNavigate = true
#Override
protected void onStop() {
shouldNavigate = false;
super.onStop();
finish();
}
And then do a check when navigating to your MainActivity:
if (shouldNavigate){
// navigate to MainActivity
}
After several tests I found that System.exit(0); fixed the problem for me , I hope this solution helps anyone that has the same issue
If you are using countdowntimer in splash activity, then please stop that in onStop(). It will help you.
try this
System.exit(0);
try{
finish();
//code press backbutton
}catch(Exception e){
}
finally{ //inside
System.exit(0);
}
I have written webview on which there may be youtube videos. When I start video and press home button application closes (Goes in background) and I can still hear audio. Then I had to kill task manually (Using battary-doctor now !!)
I have tried onPause() and onStop() methods and tried to call finish() but it is not working.
I saw some questions where it is written it is not possible to override home button.
What I want to do is kill the activity once home button is pressed. If android is handling killing ... fine .. but just pause the youtube video and then go to background !!!
Can someone help ??
Rather than killing the activity, you should just clear the WebView. onPause() is called when the activity goes into background, so that's where you should attempt to "unload" the WebView.
#Override
protected void onPause() {
super.onPause();
mWebView.loadUrl("about:blank");
}
try
android.os.Process.killProcess(android.os.Process.myPid());
try
#Override
protected void onStop() {
super.onStop();
onDestroy();
}
EDIT: To be honest You should not try to kill apllication at pressing home button, but pause video at that moment. OnPause() method is a place where You should do.
I want to know How an activity is visible once i press home button and relaunch the activity
for eg. If an application went in background and lauch again then MyActicity.java should be visible and not the same screen on which I quit.
Please suggest the possible solution
Thanks
Monali
As you can see in the documentation the onPause() method is called, whenever your Activity is paused. Just put this code in your Activity code:
public void onPause() {
this.finish();
}
If you do so, the Activity will be closed, whenever it is paused.
I don't know if I understood well your questions but if you have 2 activities A and B. if you are in B and press home button and then when you return to the app you want to go to A.. you have to call finish() method inside onPause() method on Activity B.
I am not sure if there is a better way of doing this but I want to detect somehow what caused the application to pause.
On one particular activity that displays a tracking map if the user click back or home I want to stop GPS but if the screen goes off I want to keep the gps running I am also using a wake lock so it doesn't sleep (yes I know this probably should be in a service, but that will be v2 I'm running out of time!)
I am overriding when the back button is pressed
#Override
public void onBackPressed() {
wl.release();
this.mMyLocationOverlay.disableMyLocation();
this.mMyLocationOverlay.disableCompass();
if (mLocManager != null) {
mLocManager.removeUpdates(mLocListener);
}
super.onBackPressed();
}
but I can't find a way of doing the same for home.
Can anyone help?
Bex
in the onPause() you can call isFinishing() to know if the activity is finishing for whatever may be the reason or simply getting paused. See the doc here
But first please check whether your logic that you have written comes to onBackPressed(). write some log to confirm that that part of your code is active
If i understand you correctly. You have one activity which has some view from that activity you have started another activity. But you want when you click back key of the latest activity some actions you have to perfrom in previous activity. If this is the case you have to write logic in onResume() method. Because as soon as you click back key activity on top is finished and your focus comes to onResume() method of previous activity. donot override onKEyDown() or onBackPressed().
Donot forget to vote if you feel my response is useful for you.
Thanks
Deepak
It was my understanding, obviously wrong, that onPause() is called whenever the back button is pressed? Now in my code I've put this onPause() event:
#Override
protected void onPause(){
super.onPause();
if(!_END_GAME){
Builder _alert = new AlertDialog.Builder(this)
.setMessage("onPause, with game NOT over!");
_alert.setNeutralButton("OK.",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss(); // Kills the interface
System.runFinalizersOnExit(true);
finish();
}
});
_alert.setTitle("Your Score!");
_alert.show();
}
}
Now the problem is, the dialog does not launch what-so-ever, and then the code errors out. I put the dialog there to try to visualize where the onPause() was called and help me debug some other variables and such. Yet like I said it never even gets shown. Any ideas why this would be? Is there a function that is launched prior to onPause() when the back button is pressed? Thank you in advance for any info.
onPause will always be called when your activity is no longer in the foreground, that's guaranteed. Maybe your _END_GAME is not false? Add a debug log output to your onPause method, you'll see that it always gets called.
I should note though that displaying a dialog during onPause is extremely bad form - the user is trying to get rid of your app (could even be because of an incoming phone call). You DO NOT want a dialog then. That goes against the Android design.
In fact, the Android OS will simply short-circuit your app if you try to do lengthy shenanigans in onDestroy or onPause. Basically, if those get called, you're supposed to disappear quietly.
If you really want to intercept the back button, you can check for the button like Ted suggested, but keep in mind that your app can go to the background in many other ways - home button, selected notification, incoming phone call, etc.
You should check for the back button by overriding onKeyDown, not testing in onPause. onPause gets called whenever your activity is no longer in the background leaves the foreground; it is not necessarily finishing. (You can check isFinishing() for that.) See here for more info on handling the back key.
onPause is getting called, and your dialog is showing, just for a tiny split-second before Android finishes your app. Put log statements in there if you want to watch what is going on.
If you want to show a dialog when the back button is pressed then the easiest way (works on Android 2.1+) is to override the onBackPressed method in your activity
#Override
public void onBackPressed() {
if (gameRunning) {
// show dialog
} else {
// exit
super.onBackPressed();
}
}