Implements ads in libgdx game correctly : <p>I KNOW ITS LONG BUT I NEED HELP PLEASE.</p>

<p>I've been working (and struggling) with this libgdx game for the past month now and everything worked out fine but when I added interstitial ad in it then it became unexpectedly slow and the game glitches a lot..it works fine if i remove the code with the ads</p>

<p>How my game works is it goes from MenuState>>PlayState>>GameOverState>>PlayState>>GameOverState and so on..(all of the code are given below)</p>

<p>I've got a couple questions
(a) How do I get my game to work smoothly?
(b) Is there any way to show ads only in GameOverState?(I tried introducing a boolean and a 'if' statement on the ad but failed)
(c)How do i dispose my assets more effeciently?(They don't dispose if the classes are called too fast repeatedly)</p>

<p>If you require any more information about it, do let me know and I would be happy to do so..</p>

<p>States:</p>

<p>Main libgdx class:GrumpyDemo</p>

<pre><code>package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.TimeUtils;
import com.mygdx.game.States.AdCloseState;
import com.mygdx.game.States.GameOverState;
import com.mygdx.game.States.GameStateManager;
import com.mygdx.game.States.MenuState;

public class GrumpyDemo extends ApplicationAdapter {
    public static final int WIDTH = 480;
    public static final int HEIGHT= 800;

public static final String TITLE = "Flappy Bird";
private GameStateManager gsm;
public static long AdStart = 0;
public AdController adController;


private SpriteBatch batch;

public GrumpyDemo(AdController adController){
    this.adController = adController;
}


@Override
public void create () {
    batch = new SpriteBatch();
    gsm = new GameStateManager();
    Gdx.gl.glClearColor(1, 0, 0, 1);
    gsm.push(new MenuState(gsm));
    AdStart = TimeUtils.nanoTime();
    adController.showBannerAds();
}

@Override
public void render () {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.render(batch);
    if (TimeUtils.timeSinceMillis(AdStart)&gt; 30000){
        adController.showInterstitialAds(new Runnable() {
            @Override
            public void run() {
                gsm.pop();
                gsm.push(new AdCloseState(gsm));
            }
        });

    }
}

@Override
public void dispose () {
    batch.dispose();



}
}
</code></pre>

<p>My abstract class: State</p>

<pre><code> package com.mygdx.game.States;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;

/**
 * Created by Kronos on 28-01-2017.
 */

public abstract class State {

    protected OrthographicCamera cam;
    protected Vector3 mouse;
    protected GameStateManager gsm;

    protected State(GameStateManager gsm){
        this.gsm = gsm;
        cam = new OrthographicCamera();
        mouse = new Vector3();


    }

    protected abstract void handleInput();
    public abstract void update(float dt);
    public abstract void render(SpriteBatch sb);
    public abstract void dispose();
</code></pre>

<p>MenuState:</p>

<pre><code> private Texture background;
private Texture playBtn;
private Music music;

public MenuState(GameStateManager gsm) {
    super(gsm);
    cam.setToOrtho(false, GrumpyDemo.WIDTH / 2, GrumpyDemo.HEIGHT / 2);
    background = new Texture("bg.png");
    playBtn = new Texture("playbtn.png");
    music = Gdx.audio.newMusic(Gdx.files.internal("mainmusic.mp3"));
    music.setVolume(0.8f);
    music.play();

}

@Override
public void handleInput() {
    if (Gdx.input.justTouched()) {
        gsm.set(new PlayState(gsm));

    }


}

@Override
public void update(float dt) {
    handleInput();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(background, 0, 0);
    sb.draw(playBtn, cam.position.x - playBtn.getWidth() / 2, cam.position.y);
    sb.end();

}

@Override
public void dispose() {
    background.dispose();
    playBtn.dispose();
    music.dispose();
    System.out.println("Menu State Disposed");
}
</code></pre>

<p>PlayState:</p>

<pre><code>package com.mygdx.game.States;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.mygdx.game.GrumpyDemo;
import com.mygdx.game.Sprites.Bird;
import com.mygdx.game.Sprites.Tube;

/**
 * Created by Kronos on 28-01-2017.
 */

public class PlayState extends State {
    private static final int TUBE_SPACING = 75;
    private static final int TUBE_COUNT = 4;


    private Bird bird;
    private Texture actualGamebg;
    private Tube tube ;
    private Texture ground;
    private Vector2 groundPos1,groundPos2;
    private static final int HIGHEST_GROUND_LIMIT = -30;
    private Array&lt;Tube&gt; tubes;
    private int k;
    long startTime=0;
    private Music mainMusic;
    private Music scoreIncrease;
    private Music wingFlap;
    public BitmapFont font24;
    public String SCORE;
    public int l;
    public long gameOverStart=0;






    public PlayState(GameStateManager gsm) {
        super(gsm);
        bird = new Bird(0,300);
        actualGamebg = new Texture("bg.png");
        cam.setToOrtho(false, GrumpyDemo.WIDTH/2,GrumpyDemo.HEIGHT/2);

        tubes =new Array&lt;Tube&gt;();
        ground = new Texture("ground.png");
        mainMusic = Gdx.audio.newMusic(Gdx.files.internal("mainmusic.mp3"));
        scoreIncrease = Gdx.audio.newMusic(Gdx.files.internal("smw_coin.ogg"));
        wingFlap = Gdx.audio.newMusic(Gdx.files.internal("sfx_wing.ogg"));

        font24= new BitmapFont();
        SCORE = new String();
        fontGenerator();
        groundPos1 = new Vector2(cam.position.x -cam.viewportWidth/2, HIGHEST_GROUND_LIMIT);
        groundPos2 = new Vector2((cam.position.x - cam.viewportWidth/2) + ground.getWidth(),HIGHEST_GROUND_LIMIT);
        startTime = TimeUtils.nanoTime();
        gameOverStart = TimeUtils.millis();

        for(int i=1 ; i&lt;=TUBE_COUNT; i++)
        {

            tubes.add(new Tube(i* (TUBE_SPACING + Tube.TUBE_WIDTH)));
        }
        mainMusic.play();
        mainMusic.setVolume(0.8f);
        mainMusic.setLooping(true);

    }

    @Override
    protected void handleInput() {
        if (Gdx.input.justTouched())
            bird.jump();
        wingFlap.setLooping(false);
        wingFlap.play();
        wingFlap.setVolume(0.1f);
    }




    @Override
    public void update(float dt) {
        handleInput();

        updateGround();

        bird.update(dt);
        if (TimeUtils.timeSinceNanos(startTime) &gt; 1400000000)
        {
            Score();
            startTime = TimeUtils.nanoTime();
        }



        SCORE = String.valueOf(k);





        for(int i =0 ; i&lt; tubes.size;i++)
        {
            Tube tube= tubes.get(i);

            if (cam.position.x - (cam.viewportWidth/2) &gt; tube.getPosTopTube().x + tube.getTopTube().getWidth())
            {
                tube.reposition(tube.getPosTopTube().x + ((Tube.TUBE_WIDTH + TUBE_SPACING) *TUBE_COUNT));
            }
            if(tube.collides(bird.getBounds()))
            {

                    cam.position.x = bird.getPosition().x;
                    mainMusic.stop();

                    gsm.set(new GameOverState(gsm));
                    l = k;

            }

            else
                cam.position.x = bird.getPosition().x +80;

        }
        if (bird.getPosition().y &lt;= ground.getHeight()){

            gsm.set(new GameOverState(gsm));
           mainMusic.stop();

            l = k;

        }




        cam.update();

    }



    @Override
    public void render(SpriteBatch sb) {

        sb.setProjectionMatrix(cam.combined);
        sb.begin();
        sb.draw(actualGamebg, cam.position.x - (cam.viewportWidth/2), 0);
        sb.draw(bird.getTexture(), bird.getPosition().x , bird.getPosition().y);
        for(Tube tube: tubes) {

            sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
            sb.draw(tube.getBottomTube(), tube.getPosBottomTube().x, tube.getPosBottomTube().y);
        }
        sb.draw(ground,groundPos1.x,groundPos1.y);
        sb.draw(ground,groundPos2.x,groundPos2.y);

        font24.draw(sb,SCORE,cam.position.x -2,cam.position.y + 15);
        sb.end();

    }

    /**
     * spritebatches must be drawn in order .The one at the bottommost acts as the top layer.
     */

    @Override
    public void dispose() {
        actualGamebg.dispose();
        bird.dispose();
        font24.dispose();
        for(Tube tube: tubes)
        {
            tube.dispose();
        }
        ground.dispose();
        mainMusic.dispose();
        scoreIncrease.dispose();
        wingFlap.dispose();


        System.out.println("Play State Disposed");

    }




    private void updateGround()
    {
        if (cam.position.x-(cam.viewportWidth/2) &gt; groundPos1.x + ground.getWidth())
        {
            groundPos1.add(ground.getWidth()*2,0);
        }
        if (cam.position.x-(cam.viewportWidth/2) &gt; groundPos2.x + ground.getWidth())
        {
            groundPos2.add(ground.getWidth()*2,0);
        }
    }


    public void Score()
    {
        k++;
        scoreIncrease.play();
        scoreIncrease.setVolume(0.3f);

    }
    public int getL(){
        return l;
    }


    public void fontGenerator(){
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("bitmapfont/PressStart2P.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter= new FreeTypeFontGenerator.FreeTypeFontParameter();

        parameter.size=12;
        parameter.color= Color.GOLD;
        parameter.borderColor= Color.GOLDENROD;
        font24= generator.generateFont(parameter);
        font24.setUseIntegerPositions(false);
    }


    }
</code></pre>

<p>GameOverState:</p>

<pre><code> private Texture gameOver;
private Texture gameOverBg;
private Texture playAgainBtn;
private Texture ground;
private Vector2 groundPos1;
private Music gameOverMusic;
private BitmapFont totalScore;
private String STRING;
public PlayState playState;
public Boolean AdStart = true;




public GameOverState(GameStateManager gsm) {
    super(gsm);
    cam.setToOrtho(false, GrumpyDemo.WIDTH/2,GrumpyDemo.HEIGHT/2);

    gameOver = new Texture("gameover.png");
    gameOverBg =  new Texture ("bg.png");
    playAgainBtn = new Texture("playbtn.png");
    ground = new Texture("ground.png");
    AdStart = new Boolean(true);

    gameOverMusic = Gdx.audio.newMusic(Gdx.files.internal("gameoversfx.ogg"));
    groundPos1 = new Vector2(cam.position.x -cam.viewportWidth/2, -30);
    totalScore =  new BitmapFont();
    STRING = new String();
    playState = new PlayState(gsm);
    gameOverMusic.play();
    gameOverMusic.setVolume(1.0f);



}

@Override
public void handleInput() {
    if (Gdx.input.justTouched()) {

            gsm.set(new PlayState(gsm));
            gameOverMusic.stop();
            AdStart = false;

    }


}

@Override
public void update(float dt) {
    handleInput();
    STRING = "SCORE: " + playState.getL();
    fontGenerator();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(gameOverBg,0,0);
    sb.draw(gameOver, cam.position.x-gameOver.getWidth()/2 , 5*(cam.position.y/3));
    sb.draw(ground,groundPos1.x,groundPos1.y);
    sb.draw(playAgainBtn,cam.position.x-playAgainBtn.getWidth()/2,2*(cam.position.y/3));
    totalScore.draw(sb,STRING,cam.position.x - gameOver.getWidth()/4 ,5*(cam.position.y/4));

    sb.end();


}

@Override
public void dispose() {
    gameOver.dispose();
    gameOverBg.dispose();
    playAgainBtn.dispose();
    ground.dispose();
    totalScore.dispose();
    playState.dispose();

    System.out.println("Game Over State Disposed");

}

public void fontGenerator(){
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("bitmapfont/PressStart2P.ttf"));
    FreeTypeFontGenerator.FreeTypeFontParameter parameter= new FreeTypeFontGenerator.FreeTypeFontParameter();

    parameter.size=12;
    parameter.color= Color.GOLD;
    parameter.borderColor= Color.GOLDENROD;
    totalScore= generator.generateFont(parameter);
    totalScore.setUseIntegerPositions(false);
}

public Boolean getAdStart(){
    return AdStart;
}
</code></pre>

<p>Almost forgot it..My game state manager:</p>

<pre><code>  package com.mygdx.game.States;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.Stack;

/**
 * Created by Kronos on 28-01-2017.
 */

public class GameStateManager {

    private Stack&lt;State&gt; states;
    public GameStateManager(){

        states = new Stack&lt;State&gt;();
    }


    public void push(State state)
    {
        states.push(state);
    }

    public void pop()
    {
        states.pop().dispose();
    }

    public void set(State state)
    {
        states.pop().dispose();
        states.push(state);
    }

    public void update(float dt)
    {
        states.peek().update(dt);
    }

    public void render(SpriteBatch sb)
    {
        states.peek().render(sb);
    }
}
</code></pre>

<p>Thanks for all the help.</p>
