按下后退按钮后活动意外关闭

时间:2015-12-28 02:15:37

标签: android-activity countdowntimer

我正在尝试诊断一个错误,当用户想要使用后退按钮退出游戏活动时,它会关闭窗口,但似乎并没有真正关闭活动。我认为活动没有结束的原因是因为当一个新的游戏活动开始时,活动似乎从CountDownTimer调用onFinish并关闭,好像时间用完了,当游戏中的计时器显示新的时间还剩下时间游戏。基本上我认为我的旧活动正在关闭我的新活动,即使活动不再显示也应该根据我的onBackPressed()方法关闭。

这是我的代码:

public class Game extends Activity {
ImageButton position0;
ImageButton position1;
ImageButton position2;
ImageButton position3;
Button colorPosition;
RelativeLayout background;
Random rand = new Random();
int[] position = {0,1,2,3};
int score = 0;
int streak = 1;
int multi = 1;
int currentColor;
SharedPreferences gamePrefs;
boolean countDisable = true;
public static final String GAME_PREFS = "ColorMatchFile";
TextView scoreText;
TextView multiplierText;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    gamePrefs = getSharedPreferences(GAME_PREFS, 0);
    scoreText =  (TextView) findViewById(R.id.score);
    multiplierText= (TextView) findViewById(R.id.multiplier);
    background = (RelativeLayout) findViewById(R.id.background);
    StartCount start_count = new StartCount(5000, 1000);
    start_count.start();
    colorPosition = (Button)findViewById(R.id.colorPosition);
    position0 = (ImageButton)findViewById(R.id.position0);
    position1 = (ImageButton)findViewById(R.id.position1);
    position2 = (ImageButton)findViewById(R.id.position2);
    position3 = (ImageButton)findViewById(R.id.position3);
    if(savedInstanceState != null){
        int exScore = savedInstanceState.getInt("score");
        scoreText.setText("Score: "+ exScore);
    }

@Override
public void onBackPressed() {
    setHighScore();
    finish();
    Intent intent = new Intent(getApplicationContext(), score_screen.class);
    startActivity(intent);
    super.onBackPressed();
}

public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture,long countDownInterval){
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Times Up!");
        Thread thread = new Thread();
        try {
            thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setHighScore();
        finish();
        Intent intent = new Intent(getApplicationContext(), score_screen.class);
        startActivity(intent);
    }

    @Override
    public void onTick(long millisUntilFinished){
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Left: " + millisUntilFinished/1000);
    }
}

    public class StartCount extends CountDownTimer{
    public StartCount(long millisInFuture,long countDownInterval){
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Begin!");
        MyCount counter = new MyCount(30000, 1000);
        currentColor = rand.nextInt(4);
        setCurrentColor(currentColor);
        countDisable = false;
        counter.start();
    }

    @Override
    public void onTick(long millisUntilFinished){
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Start In: " + millisUntilFinished/1000);
        showCurrentPosition();
    }
}

1 个答案:

答案 0 :(得分:0)

问题是我没有关闭MyCount的计数器。以下是我解决它的方法:

@Override
public void onBackPressed() {
    setHighScore();
    counter.cancel();
    this.finish();
    Intent intent = new Intent(getApplicationContext(), score_screen.class);
    startActivity(intent);
}

注意我必须使用counter.cancel()并使计数器成为全局变量。