-1

I'm trying to load an Intertstitial Ad in an activity and show it in a different one using Java and the AdMob SDK version 20. Does anybody know how I can do that?

1st activity: StickerPackListActivity

public class StickerPackListActivity extends AddStickerPackActivity {
public static final String EXTRA_STICKER_PACK_LIST_DATA = "sticker_pack_list";
private static final int STICKER_PREVIEW_DISPLAY_LIMIT = 5;
private LinearLayoutManager packLayoutManager;
private RecyclerView packRecyclerView;
private StickerPackListAdapter allStickerPacksListAdapter;
private WhiteListCheckAsyncTask whiteListCheckAsyncTask;
private ArrayList<StickerPack> stickerPackList;
private AdView mBanner1;
private AdView mBanner2;
private AdView mBanner3;
private AdView mBanner4;
public static InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sticker_pack_list);
    packRecyclerView = findViewById(R.id.sticker_pack_list);
    stickerPackList = getIntent().getParcelableArrayListExtra(EXTRA_STICKER_PACK_LIST_DATA);
    showStickerPackList(stickerPackList);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(getResources().getQuantityString(R.plurals.title_activity_sticker_packs_list, stickerPackList.size()));
    }

                        //Questa parte fa andare i banner, non eliminare.
    mBanner1 = findViewById(R.id.adViewtoplist);
    mBanner2 = findViewById(R.id.adViewbottomlist);
    mBanner3 = findViewById(R.id.adViewbottomlist2);
    mBanner4 = findViewById(R.id.adViewbottomlist3);
    AdRequest adRequest = new AdRequest.Builder().build();
    mBanner1.loadAd(adRequest);
    mBanner2.loadAd(adRequest);
    mBanner3.loadAd(adRequest);
    mBanner4.loadAd(adRequest);
                        //End
    InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    mInterstitialAd = interstitialAd;
                }
            });
}

Second Activity: StickerPackDetailsActivity

public class StickerPackDetailsActivity extends AddStickerPackActivity {

/**
 * Do not change below values of below 3 lines as this is also used by WhatsApp
 */
public static final String EXTRA_STICKER_PACK_ID = "sticker_pack_id";
public static final String EXTRA_STICKER_PACK_AUTHORITY = "sticker_pack_authority";
public static final String EXTRA_STICKER_PACK_NAME = "sticker_pack_name";

public static final String EXTRA_STICKER_PACK_WEBSITE = "sticker_pack_website";
public static final String EXTRA_STICKER_PACK_EMAIL = "sticker_pack_email";
public static final String EXTRA_STICKER_PACK_PRIVACY_POLICY = "sticker_pack_privacy_policy";
public static final String EXTRA_STICKER_PACK_LICENSE_AGREEMENT = "sticker_pack_license_agreement";
public static final String EXTRA_STICKER_PACK_TRAY_ICON = "sticker_pack_tray_icon";
public static final String EXTRA_SHOW_UP_BUTTON = "show_up_button";
public static final String EXTRA_STICKER_PACK_DATA = "sticker_pack";

private RecyclerView recyclerView;
private GridLayoutManager layoutManager;
private StickerPreviewAdapter stickerPreviewAdapter;
private int numColumns;
private View addButton;
private View alreadyAddedText;
private StickerPack stickerPack;
private View divider;
private WhiteListCheckAsyncTask whiteListCheckAsyncTask;
public static InterstitialAd mInterstitialAd;

private static final String TAG = "MainActivity";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sticker_pack_details);

    boolean showUpButton = getIntent().getBooleanExtra(EXTRA_SHOW_UP_BUTTON, false);
    stickerPack = getIntent().getParcelableExtra(EXTRA_STICKER_PACK_DATA);
    TextView packNameTextView = findViewById(R.id.pack_name);
    TextView packPublisherTextView = findViewById(R.id.author);
    ImageView packTrayIcon = findViewById(R.id.tray_image);
    TextView packSizeTextView = findViewById(R.id.pack_size);

    addButton = findViewById(R.id.add_to_whatsapp_button);
    alreadyAddedText = findViewById(R.id.already_added_text);
    layoutManager = new GridLayoutManager(this, 1);
    recyclerView = findViewById(R.id.sticker_list);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(pageLayoutListener);
    recyclerView.addOnScrollListener(dividerScrollListener);
    divider = findViewById(R.id.divider);
    if (stickerPreviewAdapter == null) {
        stickerPreviewAdapter = new StickerPreviewAdapter(getLayoutInflater(), R.drawable.sticker_error, getResources().getDimensionPixelSize(R.dimen.sticker_pack_details_image_size), getResources().getDimensionPixelSize(R.dimen.sticker_pack_details_image_padding), stickerPack);
        recyclerView.setAdapter(stickerPreviewAdapter);

    }
    packNameTextView.setText(stickerPack.name);
    packPublisherTextView.setText(stickerPack.publisher);
    packTrayIcon.setImageURI(StickerPackLoader.getStickerAssetUri(stickerPack.identifier, stickerPack.trayImageFile));
    packSizeTextView.setText(Formatter.formatShortFileSize(this, stickerPack.getTotalSize()));
    addButton.setOnClickListener(v -> addStickerPackToWhatsApp(stickerPack.identifier, stickerPack.name));

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(showUpButton);
        getSupportActionBar().setTitle(showUpButton ? getResources().getString(R.string.title_activity_sticker_pack_details_multiple_pack) : getResources().getQuantityString(R.plurals.title_activity_sticker_packs_list, 1));
    }
}

private void launchInfoActivity(String publisherWebsite, String publisherEmail, String privacyPolicyWebsite, String licenseAgreementWebsite, String trayIconUriString) {
    Intent intent = new Intent(StickerPackDetailsActivity.this, StickerPackInfoActivity.class);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_ID, stickerPack.identifier);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_WEBSITE, publisherWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_EMAIL, publisherEmail);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_PRIVACY_POLICY, privacyPolicyWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_LICENSE_AGREEMENT, licenseAgreementWebsite);
    intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_TRAY_ICON, trayIconUriString);
    startActivity(intent);
}

Both activities are cut short as I'd like to both load and show the ad respectively with the OnCreate method of the first and the second activity.

MrCarciofo
  • 11
  • 2

2 Answers2

1

First Activity Load Ad

public static InterstitialAd mInterstitialAd;

Now Create Function

public void InterstitialAd_load(){
AdRequest adRequest = new AdRequest.Builder().build();

      InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
        new InterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;
        Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.i(TAG, loadAdError.getMessage());
        mInterstitialAd = null;
      }
    });
}

Create Static Method to show Ad

public static void showAd(){
if (mInterstitialAd != null) {
  mInterstitialAd.show(MyActivity.this);
}
}

Second Activity import class

import com.your.packagename.firstactivity.showad;

Second Activity call method

ShowAd();
Manees
  • 51
  • 6
  • I tried to do that. However, when the second activity loads, the app crashes – MrCarciofo Dec 19 '21 at 17:52
  • kindly post your code here so i can give you a solution – Manees Dec 21 '21 at 06:32
  • Try "make a method to show ad with static keyword and call it on next activity" – Manees Dec 21 '21 at 06:35
  • You can find it in the updated question – MrCarciofo Dec 24 '21 at 10:49
  • Check the updated Answer – Manees Dec 25 '21 at 17:18
  • It's giving me some problems. For the first activity, when creating the method, it does not allow me to put the name of the first activity in `mInterstitialAd.show(MyActivity.this);`. It says that I cannot refernce it from a static context. In the second activity, when importing the class, it says it cannot resolve the symbol `showad`. I also tried to change the capital letters but nothing. And in the end, the method `ShowAd();` cannot be resolved. I tried changing capitalization in the various names, but it didn't help. – MrCarciofo Dec 26 '21 at 20:09
  • try this Declare on Top 'Activity activity;' replace myactivity.this to activity 'mInterstitialAd.show(activity);' – Manees Dec 28 '21 at 04:06
  • Still says that the argument `activity` is non-static so it cannot be referenced in a static context. All the other problems persist. – MrCarciofo Jan 05 '22 at 10:21
  • Please help @Manees :(((((((((((( – MrCarciofo Jan 08 '22 at 17:42
0

If you mean you want to click a button to open another activity then the ad show up then it's quite easy you can use this line in your onclick of the button that open the other activity

    mInterstitialAd.show(YourCurrentActivity.this); // note that mInterstitialAd is the Ad id
ElG0hary
  • 19
  • 4