Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:58+00:00 2026-06-14T21:29:58+00:00

I am trying to establish a runnable which can load ads by every 5

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:30:00+00:00Added an answer on June 14, 2026 at 9:30 pm

    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()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to establish whether prototype can do something like $$('#ID a:last').css('color','#111'); Any ideas
im trying to establish and access a session from my servlet, but I can't
I am trying to establish connection between ZABBIX SERVER which has been installed on
I have two tables between which I am trying to establish a 1-to-1 mapping
I'm trying to establish a SerialPort connection which transfers 16 bit data packages at
I'm trying to establish more formal requirements and testing procedures then we have now,
I'm trying to establish a second database connection to another database on another server.
I'm trying to establish workflows for creating new projects and for setting old projects
I'm trying to establish a relationship between two models in Rails but I am
I am trying to establish upper / lower bound in my stored procedure below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.