I want to add interstitial ad between two activities. My first activity is recyclerView. When I click on it I open another activity.
I have two working solutions, but neither gives me the performance I want. Issue in both cases is that the ad takes time to load.
My ideal solution, based on my second solution, would be this: user clicks on recyclerview item, loader appears, interstitial add is shown. I have added a loader in my onClick, and it appears when I open the item directly, but it doesn't when I open the interstitial.
This is my current code based on my second solution:
//region Item touch listener
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), listView, new itemsFragment.ClickListener() {
@Override
public void onClick(View view, int position) {
Item item = itemList.get(position);
String itemId = item.getId();
//STARTING LOADER HERE
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setCancelable(false);
progress.show();
//OPEN FOR CONDITION
if ((counter % 2) == 0) {
//LOADER NOT VISIBLE
loadInterstitialAd(itemId);
} else {
//LOADER VISIBLE
fetchItem(itemId);
}
//CLOSING LOADER HERE
progress.dismiss();
}
}));
//endregion
...
//region Interstitial ad
private void loadInterstitialAd(String itemId) {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(SAMPLE_INTERSTITIAL_ID);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.e(TAG, "Loaded interstitial ad");
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Log.e(TAG, "Failed to load interstitial ad");
}
@Override
public void onAdClosed() {
super.onAdClosed();
fetchItem(itemId);
}
});
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
//endregion
I got this to work based on Selvin's comment, by moving the show progress to loadInterstitial ad, and hiding it before the ad opens.