简体   繁体   中英

Intent activity keeps popping

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. Eg 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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM