android app not closing when I click on home button - android

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.

Related

Application gets hanged on pressing back button when video is buffering in video view

I am playing video on video view, the issue which i am facing is that when user press back button, the application gets stuck for some time and then onStop method is called of that activity, i don't know why the application is taking time in calling onStop method. Please can anyone help me out.
What jaydroider said is right. You have to override onBackPressed method as follows
#Override
public void onBackPressed() {
<stop your video here>
super.onBackPressed();
}
It is important that you stop the video before calling super.onBackPressed(); so that your application does not get stuck.
You should stop video on onPause() not on onBackPressed(). Because user may use the Home key while using your app. This will stop playing video whenever your screen is not visible to user.

How do I catch the home button click via a broadcast reciever or other technique and stop it?

I'm trying to build a screen-lock app. However, I'm experiencing a gaping hole in the system. The moment the user clicks/touches/presses the home button, the activity goes to the background. How do I prevent that? Is that possible?
I'd like a detailed explanation on how to do it.
No it is not possible to prevent activity goes to the background but you can catch event when you pressed home button so you can release resources or do any other stuff when activity goes to background.
you can override onStop method
#Override
protected void onStop() {
super.onStop();
}

Android Activity will not exit, onResume() is called immediately after onPause()

I seem to be running into a strange problem with an application that has two activities, both of which use mediaplayer:
I am creating an Android game. It has two activities: one for the menu (which launches on start-up) and one for the game (which launches when you select a level).
I have it setup so that if you are in-game and then hit the back-button it returns to the menu activity. Then, ideally, if you hit the back-button again it should exit the application completely. It does not. I placed LogCat debug statements in the menu activity's onPause() and onResume() methods. I can see that onResume() is being called immediately after onPause().
If I disable the music then it exits properly.
Below is my onPause() method for the menu activity. Am I not finishing the mediaplayer properly? Is there anything that would cause onResume() to be called?
GlMainMenu is my GLSurfaceView
protected void onPause() {
GlMainMenu.onPauseCalled = true;
GlMainMenu.mediaPlayer.release();
GlMainMenu.hasPlayedStartMusic = false;
if (isShowingPauseDialog) {
pauseDialog.cancel();
}
super.onPause();
MMglSurfaceView.onPause();
finish();
}
Some notes:
If I launch the app, and press the back-button right away it works fine. It only fails if I launch the app, enter the game, return to the menu, and then try to use the back-button.
using the Home button always exits properly.
In-case it makes any difference, both activities are using opengl for graphics

can't stop my app

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

Music in Webview Activity kill when press back button to home activity

I am coding the play mp3(Flash Embedded) in WebView(Second Activity), I can play the music in WebView as usually, but when I press back buttom to First Activity the music in Second Activity in WebView still playing, then I go to Second Activity again in WebView to stop the music but it's appear the new mp3 play list screen, I can't stop the music.
So, how can I kill the process when I press back button to First Activity(Home Screen)
Best Regards,
Virak
write this function in your class. actually this will override onpause.
#Override
protected void onPause() {
super.onPause();
finish();
}
This was the only thing that worked for me. To use the destroy() method of WebView in the onPause() lifecycle method.
#Override
public void onPause(){
super.onPause();
wView.destroy();
}

Categories

Resources