C
C
Chesterfield252020-04-06 13:11:16
Java
Chesterfield25, 2020-04-06 13:11:16

Why does it always open a second window?

There is an application that has two buttons in the main menu, when you click on which ads should be shown, after which the first button TV1 second TV2 opens a new window. It shows ads without problems, but it always opens only TV2, regardless of which button I pressed. The only exception is the moment if the advertisement does not have time to load, then it opens normally.
Here is the MainActivity code:

package com.buratinoapps.imusictv;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class MainActivity extends AppCompatActivity {

    private long backPressedTime;
    private Toast backToast;
    AdView adView;
    private InterstitialAd mInterstitialAd;


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

        Button buttonMusicBox = findViewById(R.id.btn_music_box);
        buttonMusicBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }
                else {
                    try {
                        Intent intent = new Intent(MainActivity.this, MusicBoxTv.class);
                        startActivity(intent);finish();
                    }
                    catch (Exception e){

                    }
                }
            }
        });

        Button buttonMusicTHT = findViewById(R.id.btn_tnt_music);
        buttonMusicTHT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }
                else {
                    try {
                        Intent intent = new Intent(MainActivity.this, THTMusic.class);
                        startActivity(intent);finish();
                    }
                    catch (Exception e){

                    }
                }
            }
        });

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });


        adView = findViewById(R.id.adHomeBanner);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        AdRequest adRequest1 = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest1);

        mInterstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed(){
                try{
                    Intent intent = new Intent(MainActivity.this, MusicBoxTv.class); startActivity(intent); finish();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }

        });

        mInterstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed(){
                try{
                    Intent intent = new Intent(MainActivity.this, THTMusic.class); startActivity(intent); finish();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

//Системная кнопка начало

    @Override
    public void onBackPressed() {
        if(backPressedTime + 2000 > System.currentTimeMillis()){
            super.onBackPressed();
            return;
        } else{
            backToast = Toast.makeText(this, "Click again to exit", Toast.LENGTH_SHORT);
            backToast.show();
        }
        backPressedTime = System.currentTimeMillis();
    }

    //Системная кнопка конец
}


If I'm not mistaken, the problem is in this code, but how to fix it?
mInterstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed(){
                try{
                    Intent intent = new Intent(MainActivity.this, MusicBoxTv.class); startActivity(intent); finish();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }

        });

        mInterstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed(){
                try{
                    Intent intent = new Intent(MainActivity.this, THTMusic.class); startActivity(intent); finish();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chesterfield25, 2020-04-06
@Chesterfield25

I solved the problem by changing the above code to this:

package com.buratinoapps.imusictv;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class MainActivity extends AppCompatActivity {

    private long backPressedTime;
    private Toast backToast;
    AdView adView;
    private InterstitialAd mInterstitialAd;


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

        Button buttonMusicBox = findViewById(R.id.btn_music_box);
        buttonMusicBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                    mInterstitialAd.setAdListener(new AdListener(){
                        @Override
                        public void onAdClosed(){
                            try{
                                Intent intent = new Intent(MainActivity.this, MusicBoxTv.class); startActivity(intent); finish();
                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        }

                    });
                }
                else {
                    try {
                        Intent intent = new Intent(MainActivity.this, MusicBoxTv.class);
                        startActivity(intent);finish();
                    }
                    catch (Exception e){

                    }
                }
            }
        });

        Button buttonMusicTHT = findViewById(R.id.btn_tnt_music);
        buttonMusicTHT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                    mInterstitialAd.setAdListener(new AdListener(){
                        @Override
                        public void onAdClosed(){
                            try{
                                Intent intent = new Intent(MainActivity.this, THTMusic.class); startActivity(intent); finish();
                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    });
                }
                else {
                    try {
                        Intent intent = new Intent(MainActivity.this, THTMusic.class);
                        startActivity(intent);finish();
                    }
                    catch (Exception e){

                    }
                }
            }
        });

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });


        adView = findViewById(R.id.adHomeBanner);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        AdRequest adRequest1 = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest1);





        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

//Системная кнопка начало

    @Override
    public void onBackPressed() {
        if(backPressedTime + 2000 > System.currentTimeMillis()){
            super.onBackPressed();
            return;
        } else{
            backToast = Toast.makeText(this, "Click again to exit", Toast.LENGTH_SHORT);
            backToast.show();
        }
        backPressedTime = System.currentTimeMillis();
    }

    //Системная кнопка конец
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question