Search code examples
androidadmobscreenfreezeunlock

Android Studio AdRequest on screen unlock causes app ui freeze


I have looked all over the internet and have not found a page that quite matches the behavior I'm seeing nor how to replicate it on command.

I'm using admob with my app and have a function that is called in onCreate() which runs the following code:

AdRequest adRequest = new AdRequest.Builder().build();
        RewardedAd.load(this, getString(R.string.reward_ad_ID_current), adRequest, new RewardedAdLoadCallback(){
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Handle the error.
                rewardedAd = null;
            }

            @Override
            public void onAdLoaded(@NonNull RewardedAd rewardedAd_) {
                rewardedAd = rewardedAd_;
                rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                    @Override
                    public void onAdShowedFullScreenContent() { // Called when ad is shown.
                        rewardedAd = null;
                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                        // Called when ad fails to show.
                    }

                    @Override
                    public void onAdDismissedFullScreenContent() { // Called when ad is dismissed.
                        LoadRewardedAd();
                    }
                });
            }
        });

this code works fine when the activity runs but if the screen is locked and the activity is purged to free up space then when the screen is unlocked onCreate() is called again and then this code freezes the UI with a black page

sometimes the black page is replaced with a Ad inspector page showing my list of ad units (though the ui is still frozen) see attached image:

enter image description here


Solution

  • never mind, I figured out the issue. I had a setting on admob that uses a gesture detector to show the ad inspector page. during the time the phone is locked it gets shaken up and then when the screen is unlocked it freezes the ui trying to open that page