Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Fixes #154:Timer doesn't stops in rapidfire #168

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.peacecorps.malaria.activities;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -34,6 +36,7 @@ public class RapidFireGame extends Activity{
private String resultString;
private int quesNo;
private int gameScore;
private long millisLeft;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private long timercount;
Expand Down Expand Up @@ -72,6 +75,7 @@ void initializeGame(){
scoreTv.setText("Score : " + gameScore);
askQuestion(quesNo);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void askQuestion(int i){
opt1.setBackground(getResources().getDrawable(R.drawable.info_hub_button));
opt2.setBackground(getResources().getDrawable(R.drawable.info_hub_button));
Expand All @@ -94,6 +98,7 @@ void askQuestion(int i){
}
public View.OnClickListener optionOneClick() {
return new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
counter.cancel();
Expand Down Expand Up @@ -238,20 +243,33 @@ public RapidFireTimeCounter(long millisInFuture, long countDownInterval) {

@Override
public void onTick(long l) {
timercount=l;
timer.setText(""+ l/1000);

timer.setText(""+l/1000);
millisLeft=l;
}

@Override
public void onFinish() {
counter.cancel();
prepNextQues();
}
}


@Override
protected void onPause() {
counter.cancel();
counter= null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply proper spacing between the instance and assignment operator. Here and at other places too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok @chhavip Thank you.I will take care of this from the next time. :)

super.onPause();
}

@Override
protected void onResume() {
if(counter==null){
counter= new RapidFireTimeCounter(millisLeft,1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new timer every time is not the best way to go. Utilize .start and .cancel in onPause and onResume and work with a single timer. Also I dont believe there is a need to set the timer to null while cancelling it.

Copy link
Author

@HimanshuS01 HimanshuS01 Jan 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chhavip CountDownTimer once cancelled can't be started again from the same point using the same object. We have to follow this approach only (Cancelling and then creating new Timer which will be given initial time as the time that was remaining i.e millisLeft) :)
As in the mentioned link:
http://stackoverflow.com/questions/15443219/how-to-pause-and-resume-a-timertask-timer?rq=1

Copy link
Author

@HimanshuS01 HimanshuS01 Jan 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also @chhavip I have provided the null check in onResume() because when game starts for first time , both onCreate() and onResume() are called before onStart() of activity => If I don't provide null check, it will create 2 timers - one in onCreate() and other in onResume()
also
in onPause() I have to set timer as null because in onResume() I am providing null check, if I won't set null it won't execute the code in onResume(). :) Thanks

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have a problem with the null check. The timer does not need to set to null and reinitialised is all I am saying. You can cancel it in onPause and then in onResume set the time from sharedPreffs and start it again.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please search for alternate solutions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HimanshuS01 As mentioned,could you come up with another solution?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for replying late. I was stuck in my exams . Will update you soon with another solution :-)

counter.start();
}
super.onResume();
}
public void showDialog(){
final Dialog alertDialog = new Dialog(RapidFireGame.this,android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
alertDialog.setContentView(R.layout.game_over_dialog);
Expand Down