当主页按钮按下一次时,游戏不会重新开始。

时间:2015-07-29 17:33:16

标签: java android android-homebutton

我正在开发一款游戏。当我按下设备上的主页按钮时,我的游戏就关闭了。当我再次启动它时,我的游戏才开始,直到我清除了设备的内存。 请提前解决我的问题。 这是我的游戏主要活动:

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

1 个答案:

答案 0 :(得分:2)

必须完全了解Android App的活动生命周期。

仔细阅读本页:

https://developer.android.com/reference/android/app/Activity.html

您的代码中发生的事情是您只有onCreate()和onPause()。

您需要添加代码才能在切换回应用后正确重新启动应用。

必须在onRestart()中添加代码。将代码从onPause()移动到onStop()代替。

所以你将得到onStop()的循环 - > onRestart() - > onStop() - > onRestart()等......

单击主页按钮onStop()将被调用,当你返回它时,将调用onRestart()。