-
Notifications
You must be signed in to change notification settings - Fork 110
Fixes #154:Timer doesn't stops in rapidfire #168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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; | ||
|
@@ -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)); | ||
|
@@ -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(); | ||
|
@@ -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; | ||
super.onPause(); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
if(counter==null){ | ||
counter= new RapidFireTimeCounter(millisLeft,1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please search for alternate solutions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HimanshuS01 As mentioned,could you come up with another solution? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)