按一次主页按钮后游戏不会重新开始。
Game not start again when Home button pressed once's.
我正在开发一款游戏。当我按下设备上的主页按钮时,我的游戏关闭了。当我再次启动它时,我的游戏不会开始,直到我清除了设备的内存。
请提前解决我的问题。
这是我的游戏主要活动:
public class Game extends Activity {
MediaPlayer backgroungMusic;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_game);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//turn title off
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
backgroungMusic = MediaPlayer.create(Game.this, R.raw.music);
backgroungMusic.setLooping(true);
backgroungMusic.start();
}
@Override
protected void onPause()
{
super.onPause();
backgroungMusic.release();
finish();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// GamePanel.thread.setStoped(true);
GamePanel.thread.setRunning(false);
// in the next line of code we also style the dialog through xml which i put in styles
AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
alertDialog.setTitle("Exit Alert");
alertDialog.setMessage("Do you really want to exit the Game?");
alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
// A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
finish();
System.exit(0);
return;
}
});
alertDialog.setButton2("Cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// dialog.cancel();
// GamePanel.thread.resume();
dialog.dismiss();
// When user press the "Cancle" button then game resume for 3 seconds then start again
// Here is the Code of the toasts and each toast appear with delay of one second.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(60);
textView.setText("READY!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 2.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("3");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 2500);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 2.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("2");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 5000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 3. Init game.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("1");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 7500);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 3. Init game.
GamePanel.thread.setRunning(true);
}
}, 10000);
return;
}
}
);
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
充分了解 Android 应用程序的 activity 生命周期至关重要。
仔细阅读此页:
https://developer.android.com/reference/android/app/Activity.html
您的代码中发生的情况是您只有 onCreate() 和 onPause()。
您需要添加代码以在切换回应用后正确重启您的应用。
必须在onRestart() 中添加代码。将您的代码从 onPause() 移至 onStop()。
所以你会得到 onStop() -> onRestart() -> onStop() -> onRestart() 等等的循环...
当您单击主页按钮时将调用 onStop(),当您返回时将调用 onRestart()。
我正在开发一款游戏。当我按下设备上的主页按钮时,我的游戏关闭了。当我再次启动它时,我的游戏不会开始,直到我清除了设备的内存。 请提前解决我的问题。 这是我的游戏主要活动:
public class Game extends Activity {
MediaPlayer backgroungMusic;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_game);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//turn title off
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
backgroungMusic = MediaPlayer.create(Game.this, R.raw.music);
backgroungMusic.setLooping(true);
backgroungMusic.start();
}
@Override
protected void onPause()
{
super.onPause();
backgroungMusic.release();
finish();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// GamePanel.thread.setStoped(true);
GamePanel.thread.setRunning(false);
// in the next line of code we also style the dialog through xml which i put in styles
AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
alertDialog.setTitle("Exit Alert");
alertDialog.setMessage("Do you really want to exit the Game?");
alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
// A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
finish();
System.exit(0);
return;
}
});
alertDialog.setButton2("Cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// dialog.cancel();
// GamePanel.thread.resume();
dialog.dismiss();
// When user press the "Cancle" button then game resume for 3 seconds then start again
// Here is the Code of the toasts and each toast appear with delay of one second.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(60);
textView.setText("READY!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 2.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("3");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 2500);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 2.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("2");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 5000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 3. Init game.
toast = new Toast(Game.this);
TextView textView = new TextView(Game.this);
textView.setTextColor(Color.DKGRAY);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(140);
textView.setText("1");
// textView.setText("done!");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
toast.show();
}
}, 7500);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// show toast 3. Init game.
GamePanel.thread.setRunning(true);
}
}, 10000);
return;
}
}
);
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
充分了解 Android 应用程序的 activity 生命周期至关重要。
仔细阅读此页:
https://developer.android.com/reference/android/app/Activity.html
您的代码中发生的情况是您只有 onCreate() 和 onPause()。
您需要添加代码以在切换回应用后正确重启您的应用。
必须在onRestart() 中添加代码。将您的代码从 onPause() 移至 onStop()。
所以你会得到 onStop() -> onRestart() -> onStop() -> onRestart() 等等的循环...
当您单击主页按钮时将调用 onStop(),当您返回时将调用 onRestart()。