简体   繁体   中英

Is it okay to use/invoke new Handler() in a loop?

I am using new Handler().postDelayed to set a delay when reloading an ad when the previous request of the ad has failed in the requestNewInterstitial method. I am wondering if invoking/creating many new Handlers would be okay as in my loop the new Handler line of code could be run up to 5 times. My question is, if later I have to clear the handler in the Activity onDestroy method so that there are no memory leaks, should I clear it only once or should I clean all the new Handlers I have created (the number of times that the loop is run). Also, how could I implement to clear the Handler OnDestroy method?

This is my code:

 @Override
      protected void onResume() {
        super.onResume();
        requestNewInterstitial(5);

        }


 private void requestNewInterstitial(int maxRetry) {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(Activityone.this, getString(R.string.interid),
                adRequest, new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        mInterstitialAd = interstitialAd;
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {


          if (maxRetry>0){
                         
                    new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              mInterstitialAd = null;
              requestNewInterstitial(maxRetry-1);

          }
      }, 1000);
              
                   
                    }
                });
                     }
           
    }




  btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
                if (mInterstitialAd != null) {

                    mInterstitialAd.show(Activityone.this);

                    mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                        @Override
                        public void onAdDismissedFullScreenContent() {
                  
                        }

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {

                        }

                    });

Thanks for the help.

If the delay in postDelay extends beyound the activity lifetime, the runnable will execute after the activity has been destryed and will lead app crash. You should use a common handler. To clear the handler, you can call handler.removeCallbacksAndMessages(null); in on destroy or wherever else you want to clear them.

Handler handler;

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

    handler = new Handler();
    btnPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mInterstitialAd != null) {

                mInterstitialAd.show(Activityone.this);

                mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                    @Override
                    public void onAdDismissedFullScreenContent() {

                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {

                    }

                });
            }
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    requestNewInterstitial(5);

}

@Override
protected void onDestroy() {
    handler.removeCallbacksAndMessages(null)
    super.onDestroy();
}


private void requestNewInterstitial(int maxRetry) {
    AdRequest adRequest = new AdRequest.Builder().build();
    InterstitialAd.load(Activityone.this, getString(R.string.interid), adRequest, new InterstitialAdLoadCallback() {
        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
            mInterstitialAd = interstitialAd;
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {


            if (maxRetry > 0) {

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mInterstitialAd = null;
                        requestNewInterstitial(maxRetry - 1);

                    }
                }, 1000);


            }
        }
    });
}

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