View Interstitial by clicking the Toolbar home button

Asked

Viewed 130 times

0

I tried to implement the display of an interstitial banner on the home button inserir a descrição da imagem aqui 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;
}

1 answer

2


Henqsan,

You should use the id below which is the correct id for the Activity back button.

    if (id == android.R.id.home){
         doSomethink();
    }

Remembering that if you are using Toolbar has arrow-there.

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(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.

  • but this solved your problem ?

  • Unfortunately did not resolve.

  • 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 the return true. Now it’s working right. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.