Reputation: 23
I set a timer to this application, so when the timer ends and boxes in Itemdata are still selected, it should call for Thanks.class activity. However, I realised that it class the Thanks.class activity multiple times. E.g. if there are 3 selected items, it calls the Thanks.class activity three times.
This is the timer i've declared
//Declare timer
CountDownTimer cTimer = null;
//start timer function
void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
Here is how I call the function.
@Override
public View getView(final int position, final View view, ViewGroup parent) {
final GridItemData itemData = getItem(position);
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.grid_image, parent, false);
ivGallery = itemView.findViewById(R.id.grid_item_image);
ivGallery.setImageResource(getItem(position).imageUrl);
final MainActivity main = new MainActivity();
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivGallery = itemView.findViewById(R.id.grid_item_image);
itemData.setSelected(!itemData.isSelected());
startTimer();
if(itemData == getItem(8)) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
customizeAlert(builder, v);
builder.show();
}
else if (itemData == getItem(0)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickfloor: R.drawable.floor);
}
else if (itemData == getItem(1)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cb: R.drawable.wb);
}
else if (itemData == getItem(2)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cs: R.drawable.s);
}
else if (itemData == getItem(3)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickothers: R.drawable.others);
}
}
});
return itemView;
}
Please advice me what to do, thank you!
Upvotes: 0
Views: 48
Reputation: 131
Thats because you start a CountdownTimer for each click but you don't check if there is already a CountDownTimer active which needs to be canceled or restarted.
If you expand this to production i would recommend to use AsyncTasks or Courotines for this, because when your CountDownTimer is finished your are maybe no longer allowed to change the UI-State and you will get an IllegalStateException or your findByView will throw a NullPointerException. With AsyncTasks and Courotines you also don't need to worry about spawning multiple instances of your CountDownTimer. https://developer.android.com/reference/android/os/AsyncTask
Upvotes: 1
Reputation: 76799
How about starting that CountDownTimer
only once?
void startTimer() {
if(cTimer == null) {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
}
Alternatively, just stop the first CountDownTimer
, when the second one is about to be started.
Upvotes: 2