Счетчик таймера не останавливается даже после завершения активности - PullRequest
0 голосов
/ 30 декабря 2018

У меня есть приложение для викторины, в котором есть таймер для всей игровой активности, где вы должны ответить как 20 вопросов, и у каждого вопроса есть 10 секунд.

после того, как назначенные 20 закончились, это займетВы к результатам деятельности, которая показывает ваш счет.даже после вызова счета активности отобразится тост из основного актива, и счет активности откроется снова и снова.

int score = 0;
int unanswer = 0;
int questionasked = 1;
int maxquestion = 20;
TextView first, operator, second, timeview;
Button A, B, C, D;
int ans = 0;
String t;
DecimalFormat df;
CountDownTimer mCountDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeview = findViewById(R.id.time);
    A = findViewById(R.id.A);
    A.setOnClickListener(this);
    B = findViewById(R.id.B);
    B.setOnClickListener(this);
    C = findViewById(R.id.C);
    C.setOnClickListener(this);
    D = findViewById(R.id.D);
    D.setOnClickListener(this);
    first = findViewById(R.id.first);
    second = findViewById(R.id.second);
    operator = findViewById(R.id.operator);
    df = new DecimalFormat("###.##");
    starTimer();
    fillValue();

}

основной код активности

private void fillValue() {
    if (questionasked > maxquestion)
        showscore();
    questionasked++;
    int a = (int) (Math.random() * 50 + 1);
    int b = (int) (Math.random() * 50 + 1);
    int op = (int) (Math.random() * 4 + 1);
    int buttonoption = (int) (Math.random() * 4 + 1);
    char operatorcharter = '+';
    switch (op) {
        case 1:
            ans = a + b;
            operatorcharter = '+';
            break;
        case 2:
            ans = a - b;
            operatorcharter = '-';
            break;
        case 3:
            ans = a * b;
            operatorcharter = 'X';
            break;
        case 4:
            ans = (a) / b;
            operatorcharter = '/';
            break;
        //default:
        //Toast.makeText(this,op+"",Toast.LENGTH_SHORT).show();
    }
    //Toast.makeText(this,a+" "+operatorcharter+" "+b+" "+ans+" "+buttonoption,Toast.LENGTH_LONG).show();
    operator.setText(operatorcharter + "");
    first.setText(a + "");
    second.setText(b + "");
    t = df.format(ans);
    int temp = 0;
    switch (buttonoption) {
        case 1:
            A.setText(t);
            break;
        case 2:
            B.setText(t);
            break;
        case 3:
            C.setText(t);
            break;
        case 4:
            D.setText(t);
            break;
        //default:
        // Toast.makeText(this,buttonoption+" butt",Toast.LENGTH_SHORT).show();
    }
    for (int i = 1; i <= 4; i++) {
        if (i == buttonoption)
            continue;
        temp = (int) (Math.random() * ans + 1);
        if (temp == ans) ;
        temp += 5;
        String temp1 = df.format(temp);
        if (i == 1)
            A.setText(temp1 + "");
        else if (i == 2)
            B.setText(temp1 + "");
        else if (i == 3)
            C.setText(temp1 + "");
        else if (i == 4)
            D.setText(temp1 + "");
    }
    mCountDownTimer.start();
}

private void showscore() {
    Intent i = new Intent(this, final_activity.class);
    i.putExtra(Contact.maxquestion, maxquestion);
    i.putExtra(Contact.score, score);
    i.putExtra(Contact.unanswer, unanswer);
    mCountDownTimer.cancel();
    Toast.makeText(this, "new activity open", Toast.LENGTH_LONG).show();
    startActivity(i);
    this.finish();
}

@Override
public void onClick(View v) {
    Button b = (Button) v;
    String clickbuttonvalue = (String) b.getText();
    if (clickbuttonvalue.equals(t))
        score++;
    //Toast.makeText(this,score+"  "+clickbuttonvalue   ,Toast.LENGTH_LONG).show();
    mCountDownTimer.cancel();
    fillValue();
}


public void starTimer() {
    mCountDownTimer = new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            timeview.setText("seconds remaining: " + millisUntilFinished / 1000);
            Toast.makeText(MainActivity.this, "new" + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
        }

        public void onFinish() {
            unanswer++;
            fillValue();
        }

    }.start();
}

}

это мой код даже после открытия нового действия и открытия нового метода проверки активности showscore () plz, помогите!

1 Ответ

0 голосов
/ 30 декабря 2018

Вам необходимо переопределить метод onDestroy, который будет срабатывать после завершения действия (не забудьте добавить finish() после startActivty()).

@Override
    public void onDestroy() {
    super.onDestroy();
    mCountDownTimer.cancel();
}
...