Reputation: 7798
I'm developing a game based on this tutorial . I managed to make most of the game to work, but when the user presses home button, my game keeps running in the background, and I can pause it using the onPause method, and everything but it never runs the onResume method.
When the user restarts the game, none of the methods is called, and my panel has lost the focus.
How can i make it get the focus again, after the game was sent to background after the Home Button was pressed?
My Activity Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
panel = new Panel(this);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(panel);
Log.d(TAG, "onCreate");
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onRestart(){
super.onRestart();
Log.d(TAG,"onRestart");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d(TAG, "onSaveInstance");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstance");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG,"onpause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop..");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy...");
}
My Panel Constructor :
WhackEmPanel(Context c) {
super(c);
this.context=c;
loadAnimations();
setCharacters();
getHolder().addCallback(this);
backGround = BitmapFactory.decodeResource(getResources(), R.drawable.background);
// create the game loop thread
thread = new MainThread(getHolder(),this);
setFocusable(true);
//Generate Random Numbers for characters
Picker = RandomPicker.RandomPick(8,500); //From 0 to 8, gives me 500 random integers
picked=Picker.remove(0);
}
I'm new to this gaming programming thing, but as far as I understood I need to call setFocusable(true) again, but where?
EDIT: After the suggestion from DizzyThermal I removed the pause method, leaving just the log, and I don't understand why it never reaches the onResume() method!
Upvotes: 0
Views: 2417
Reputation: 7798
I've found the solution. When the home button was pressed, my gameloop Thread was still running, onStop was never called, and I'm guessing the activity was never properly stopped. Now my onPause method actually finishes the Thread execution and I run into the Lunar Lander example bug.
This Blog helped me solving the bug by adding the following code to my surface creator:
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "surfaceCreated");
\\ <-- added fix
if(thread.getState()== Thread.State.TERMINATED){
thread = new MainThread(getHolder(),this);
thread.setRunning(true);
thread.start();
// <-- added fix
}else {
thread.setRunning(true); //original code
thread.start(); //original code
}
}
My onPause method now actually stops the game Thread execution.
public void onPause(){
super.onPause();
panel.stopPLaying();
Log.d(TAG,"onpause");
}
Upvotes: 1
Reputation: 114
Instead of making the onResume() method protected, try making it public..
Also, try moving the super.onResume();
to the top of the method.. I'm pretty sure that it needs to be the first thing called in the "Overriden" function..
Upvotes: 0