12

I am trying to establish a runnable which can load ads by every 5 sec interval (of course 5 sec is too fast, it's just for testing purpose)

Here is my code:

package com.admobsdk_dfp_handler;

import com.google.ads.*;
import com.google.ads.doubleclick.*;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;

public class AdMobSDK_DFP_Handler extends Activity {
    private DfpAdView adView;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {

        public void run() {
        adView.loadAd(new AdRequest());
        handler.postDelayed(this, 5000);
    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);

        adView = new DfpAdView(
                this,
                AdSize.BANNER,
                AD_UNIT_ID);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);

        layout.addView(adView);

        adView.loadAd(new AdRequest());

        handler.postDelayed(runnable, 5000);


    };

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_ad_mob_sdk__dfp__handler,
                menu);
        return true;
    }

}

If I press home button to hide the app to the background, the runnable keeps loading ads by 5 sec interval.

Is there any method to stop runnable when an app is hidden to the background? Many thanks.

Lokesh Mehra
  • 545
  • 6
  • 16
Kit Ng
  • 993
  • 4
  • 12
  • 24

3 Answers3

37

Simply use the onPause()

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed

@Override
protected void onPause() {
     handler.removeCallbacks(runnable);
     super.onPause();
}

Optional

If you want to resume that runnable. Just override the onResume() callback

@Override
protected void onResume()
{
      handler.postDelayed(runnable, 5000);
      super.onResume();
}

Remove also the handler.postDelayed(runnable, 5000); in onCreate()

  • BTW, the app works perfectly after removing handler.postDelayed(runnable, 5000) in onCreate(), is it because when I open the app, the method run() will start? So that I don't need to call handler again with onCreate() ? – Kit Ng Dec 04 '12 at 15:41
  • after calling ``onCreate()`` by android system, the next will be ``onStart()`` and then ``onResume()``. You can see it here, http://developer.android.com/training/basics/activity-lifecycle/starting.html –  Dec 04 '12 at 23:34
  • 1
    what if he has moved to another activity ? How does he do it there ? – tony9099 Sep 19 '13 at 13:24
2

onPause() onStop() functions you can put there handler.removeCallbacks(runnable);

You can get more info from here Look at Activity life circle

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Mikhaili
  • 161
  • 6
0

onPause() is called when the application goes in the background. Remove runnable in onPause()

protected void onPause() {
     handler.removeCallbacks(runnable);
     super.onPause();
}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • 1
    what if he has moved to another activity ? How does he do it there ? – tony9099 Sep 19 '13 at 13:25
  • 1
    @tony9099, it is the same. The actual activity has to go to background before another activity comes to foreground. onPause() will be called. – Lazy Ninja Sep 20 '13 at 00:58