Search code examples
javaandroidandroid-fragmentsrewardedvideoad

App closes when RewardedVideoAd is closed. Show Fragment onAdClose?


First of all: There is no exception thrown when this problem occurs.

My app is currently running in Google Plays closed alpha program. When I test the app using an emulator provided by Android Studio or using my own Galaxy A5, everything works fine and as expected.

Now I'm getting reports of my alpha testers, that their app is closing after they close the VideoAd. After investigating a while, I realized all devices used by google while performing Pre-Launch-Tests, had the same problem (visible by the video provided by Google).

Since there is no exception thrown, I have absolutely no idea how to debug this behaviour. Following is the code of the onRewarded method (the other methods of the listener don't contain any code):

@Override
public void onRewarded(RewardItem rewardItem) {
    int amount = calculateAdReward(rewardItem.getAmount());
    updateCoins(amount);
    rewardedAmount = amount;
    rewarded = true;
}

rewardedAmount and rewarded are private fields used to instantiate a fragment in the activitys onResume-method:

@Override
protected void onResume() {
    super.onResume();
    mAdVideo.resume(this);
    if(rewarded){
        loadFragment(RewardFragment.newInstance(rewardedAmount);
        rewardedAmount = 0;
        rewarded = false;
    }
}

My goal is to display a fragment after the user has been rewarded which holds the rewarded amount. My first attempt was as followed:

@Override
public void onRewarded(RewardItem rewardItem) {
    int amount = calculateAdReward(rewardItem.getAmount());
    updateCoins(amount);
    loadFragment(RewardFragment.newInstance(amount));
}

which failed, since I tried to instantiate a fragment after saveInstanceState was called.


Solution

  • So after a bit of research, I solved my problem.

    What I learned: Never perform transactions inside the activity's lifecycle methods (onResume,...) since there is a possibility that the activity hasn't fully resumed when you reach your transaction code.

    Instead, perform transactions in the onPostResume method. When this method is called, it is guaranteed your activity and all it's fragments have been fully resumed and it is "save" to perform a transaction.

    On a side note: Don't perform transactions in those methods if you don't absolutely have to.