问题
I was using the interstitial ad from Admob in my android app. The interstitialAd.show() is called at certain events of the app. Everything worked fine but one annoying use case with the ad.
If users click HOME while the ad is displaying or the ad activity (i.e. users click the ad) is active, the ad or the ad activity will be displayed after switching back to the app. I would prefer that the app is displayed (i.e. the main activity is at the top of the activity stack) since it looked confusing to the users to see the ad first.
I tried to trap all the activity callbacks (such as onStart(), onResume() ...), but apparently the app will never become foreground in this use case. Since the ad activity is started by calling interstitialAd.show(), the tricks I know on managing the activity stack does not help, either.
Does anyone know how to automatically close the ad and return the app main activity to the top?
Thanks.
回答1:
If anyone is looking for an answer to this. If the user hits the Home button while the interstitial ad is being shown, this prevents it from showing when the user launches the app again.
In your AndroidMainifest.xml file add: android:noHistory="true" to the AdActivity activity
Like this:
<activity android:name="com.google.android.gms.ads.AdActivity"
android:noHistory="true"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
</activity>
This seems to be working so far.
回答2:
This may be too late but you cannot close the ad automatically. A hack around it is that in your onBackPressed() method, after the user taps on the back button to close the ad, you return the user to the application and using a Toast method, alert the user to click the back button again to exit the application. This approach also satisfies Google's suggestion that you allow the user to know which app is displaying the ads, thus you return to the app before closing it finally. In terms of the user experience, it may not be the best but it sure does do the trick. Below is a sample code.
boolean doubleBackToExitPressedOnce = false;
public void onBackPressed() {
if(doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
if(backPressAd.isLoaded()) {
backPressAd.show();
backPressAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
doubleBackToExitPressedOnce = true;
Toast.makeText(getApplicationContext(), "Click BACK again to exit", Toast.LENGTH_LONG).show();
backPressAd.loadAd(new AdRequest.Builder().build());
}
});
} else {
finish();
}
}
回答3:
I don't think there is a solution.
Essentially the Ad.show() needs to open up the ad in a new Task so it is not part of the Activity list for the current Task/app.
来源:https://stackoverflow.com/questions/18044294/how-to-automatically-close-admob-interstitial-ad-if-users-click-the-android-home