java – AdMob changes the activity in android app

Question:

I would like to ask you the reason for my problem. I have many activities, 2 of them are: the main one and the game activity. I'm using AdMob, it works fine for me in other activities, the problem I have is that when I try to use it on a button within a Dialog, in which I have a button to "restart" a game, it doesn't work for me. The idea is that when you click on it, it will show the ad; and then when the ad is closed, the game will restart normally. What is happening is that when the ad is closed, it sends the main one.

 //Aquí está el evento para cerrar el anuncio
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
         //Método para reiniciar el juego
            reset_game();
        }
    });

    requestNewInterstitial();

  //Este evento es para el botón de reiniciar el juego
    ImageButton resetGame =(ImageButton) dialog.findViewById(R.id.resetGame);
    resetGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                reset_game();
                dialog.dismiss();
            }

        }
    });

    dialog.show();
   display_unlocked_card_alert();
    Log.d(  TAG,"unlocked_check value 2 = " + unlocked_check);
}

I know very well that in reset_game I do not invoke the Main or anything like that, since there I only generate the game, but I leave it anyway:

  private void reset_game(){

    HashMap data = MemoryGame.game_mode_for(mode, cards_no);
    init_table((int) data.get("rows"), (int) data.get("cols"));
    memory_game.generate((int) data.get("total_cards"));
    //init_table(4, 5);
    //memory_game.generate(4*5);
    current_turn = 0;
    select_1 = 0;
    select_2 = 0;
    c =0;

    set_timer((int) data.get("time"));
    update_game_state_view();
    update_cards_state_view();
}

I really don't know why that happens, does it have something to do with being inside a Dialog?

EDITION

I did not solve this problem, I had better change the way I see the ads. Anyway I would like to know the error or how to solve it, out of curiosity.

Upon request, I add the code from the Dialog:

//Es un dialogo personalizado
private void alert_win() {

    dialog = new Dialog(GameScreen.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    dialog.setContentView(R.layout.winner_dialog);
    dialog.setCanceledOnTouchOutside(false);


    Typeface typeFace=Typeface.createFromAsset(getAssets(), "bellosmcp.ttf");

    TextView moTextView = (TextView) dialog.findViewById(R.id.moTextView);
    moTextView.setTypeface(typeFace);
    moTextView.setText(String.valueOf(_turn_count));
    TextView matchTextView = (TextView) dialog.findViewById(R.id.matchTextView);
    matchTextView.setTypeface(typeFace);
    movesTextView.setTypeface(typeFace);
    matchTextView.setText(String.valueOf(_score));
    TextView pairstextView =(TextView) dialog.findViewById(R.id.pairstextView);
    pairstextView.setTypeface(typeFace);
    pairstextView.setText(Integer.toString(c));
    ImageView closeDialog = (ImageView) dialog.findViewById(R.id.close);
    TextView highScoreTextView = (TextView) dialog.findViewById(R.id.high_Score);
    highScoreTextView.setTypeface(typeFace);
    highScoreTextView.setText(String.valueOf(user.high_score));     



//Aquí está el botón para reiniciar

ImageButton resetGame =(ImageButton) dialog.findViewById(R.id.resetGame);
    resetGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();

//Aquí esta lo de las Ads

           if (mInterstitialAd.isLoaded()) {
               mInterstitialAd.show();
           } else {

                reset_game();
          }               

        }
    });

    dialog.show();

The question is that if it showed the Ads, but at the time of closing it, it did restart, the game but the activity was closed redirecting to the Main of the game, I could not solve that error, better I changed the way in which the ads were displayed.

Answer:

I think what happens is that when you press the Dialog button, if the Interstitial is not loaded (when you do the if isLoaded ), it invokes the method to restart the game and discards the Dialog (dismiss); but if the advertisement is shown, when closing it, you invoke the method directly without discarding the Dialog, therefore the Activity may collapse although the strange thing is that this should give you an error at the AndroidRuntime level, I don't know if this is why I mention but it is the only weird thing i notice in the code.

Try invoking the dismiss of the Dialog inside the if of the isLoaded right after launching the Interstitial with the show() , something like this:

//Este evento es para el botón de reiniciar el juego
ImageButton resetGame =(ImageButton) dialog.findViewById(R.id.resetGame);
resetGame.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            // Cierras el Dialog para evitar que la Activity colapse...
            dialog.dismiss();
        } else {
            reset_game();
            dialog.dismiss();
        }

    }
});
Scroll to Top