0
I tried to implement the display of an interstitial banner on the home button of my activitys but it didn’t work properly.
I used
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home){
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}else{
finish();
}
}
}
Only that way the banner does not appear in the click but only when closing the app.
What is the correct way to display the banner by clicking the home button?
QUESTION CORRECTED AND WORKING
Obs.: The configuration for the display of the home button I made directly in the manifest, not needing to set Toolbar in Activity itself, just include the code below to catch the action in Toolbar’s home button.
MANIFEST
<activity
android:name=".MinhaActivitySecundaria"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="adjustResize"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
INCLUDE THE CODE BELOW IN THE SECONDARY ACTIVITY
Remember to use the Appcompatactivity extension
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
if (interstitialAd.isLoaded()) {
interstitialAd.show();
finish();
}else{
finish();
}
}
return true;
}
Thanks for the help! I’m setting the back button directly in the manifest and I’m using the Appcompatactivity extension. Therefore, I do not know Toolbar in secondary activitys, only in Mainactivity.
– Henqsan
but this solved your problem ?
– Ezequiel Messore
Unfortunately did not resolve.
– Henqsan
I returned to rectify my comment made in the post of Ezekiel Messore. As well as not paying attention to the correct way to verify the id
android.R.id.home
I had forgotten to call thereturn true
. Now it’s working right. Thank you!– Henqsan