I am programming a game for android and lately I have found an issue when minimizing it. Basically the game won't stop when minimized and is still performing the code.
I override the onPause() and onResume().
#Override
public void onPause()
{
super.onPause();
((com.anderiel.atomic.defense.AtomicDefense)MyCustomView).Pause();
}
#Override
public void onResume()
{
super.onResume();
((com.anderiel.atomic.defense.AtomicDefense)MyCustomView).Resume();
}
Adding them to the code that should stop the game code when it is paused. It works quite fine if i pause the game by myself (going to pause menu). But when I minimize it and then maximize it once again, no pause happens.
Does the game continue from the point where you turn off the screen? Or does it restart completely?
If the app restarts, try adding this to your manifest:
android:configChanges= "orientation|screenSize"
Related
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 am a beginner with Android, developing my final project for University. I have a problem that I do not know how to solve or how to start with it. When I launch my app I interact with it and then i press Home button on the emulator. I do whatever else and then I press the button to see open apps and select my app and then it opens in the very exact situation it was when i left it to go to the home... That's what I wanted BUT if while running my app I change to another app or go to home and I launch the app from the menu (press menu and find my app in the grid and tap the icon) it launches the app from the launch activity, being a new instance, overriding the previos app status...
What can be the error? What info should I provide to get some guidance?
Thank you very much in advance and excuse my messy explanation...
Miguel
PD: I tried to find this same issue, but I found nothing because I do not even know what to google for.... sorry
Perhaps, you should understand the Android Activity life cycle first.
http://developer.android.com/images/activity_lifecycle.png
Then you have to override all these methods (given in the diagrom) and you should use the debugger to know how, when, which method is called and note down the activity state in every case. The methods are:
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
Then to override them, you can add your own code to maintain/save the activity sate.
The developer guide has a good introduction on this topic:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Save the state of your application inside the onPause() of the Activity. Place your code to restore the state of your application inside of onResume() inside of the Activity. This is part of the Activity lifecycle.
Just don't let the onResume() misleading language confuse you. The thing that resumes is the UI thread in this case, so this method will get called even when the application initially begins (when the UI appears).
If you have trouble fixing this, just post your Activity code in your question.
Hi i am developing a game using andengine. I am unable to pause the music when power button interrupt. In manifest file I added:
android:configChanges="orientation|keyboard|keyboardHidden"
code- onPause and onResume methods
protected void onPause() {
if(menumusic!=null)
menumusic.pause();
super.onPause();
this.mEngine.onPause() ;
}
protected void onResume() {
if(menumusic!=null)
menumusic.resume();
super.onResume();
this.mEngine.onResume();
}
Problem is when power button is pressed onPause() method is calling and immediately onResume() is calling.
Plz help me.
try this ..hope this may help u
use onWindowFocusChanged to help resume the game. it works and music does not resume until u r back in the game:
http://www.andengine.org/forums/gles2/onresume-being-called-when-device-should-be-paused-t8748.html#p42961
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
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();
}