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!
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:
My onPause method now actually stops the game Thread execution.