Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

adLoaded never changes it's value #88

Closed
TimCatana opened this issue Jan 5, 2022 · 3 comments
Closed

adLoaded never changes it's value #88

TimCatana opened this issue Jan 5, 2022 · 3 comments

Comments

@TimCatana
Copy link

Description

When using the useInterstitialAd hook, the adLoaded value initially gets returned as true. At this point, the ad is expected to be loaded. When the ad is presented, the adLoaded value is expected to change to false. However, the value does not change to false. The value remains true even though no ad is loaded. I have tested this and the show() function does not show ad because no ad is loaded. The adLoaded value however, is equal to true thus being a false indicator for whether the ad is loaded and ready to present or not.

Expected behavior

When the useInterstitialAd hook is called, it should return an adLoaded value of true if the ad is loaded correctly. When the show() function is called, the adLoaded variable should be changed to false (as the ad has been shown, and a new one needs to be loaded). The developer will most likely test if the adLoaded variable is false and the run the load() function which should change the adLoaded variable to true.

Essentially, the flow should look like this:

useInterstitialAd(...) successful ? adLoaded = true

show() successful ? adLoaded = false

load() successful ? adLoaded = true

Actual behavior & steps to reproduce

When the useInterstitialAd hook is called, it returns an adLoaded value of true. When I ran the show() function after the useInterstitialAd hook, the ad was shown but the adLoaded value remained false. The next time I ran the show() function, an ad was not shown, despite the fact that the adLoaded value remained true

In development, the developer will most likely rely on the adLoaded variable to determine whether to load() a new ad or not.

Snack or minimal code example

I've been having problems with snack and don't have time to make a repo dedicated to this issue so I'll just copy paste the code here. I have included the adPresented variable to compare against the adLoaded variable.

import React from 'react';
import {
  View,
  Text,
  StyleSheet,
  Pressable,
} from 'react-native';
import { useInterstitialAd, TestIds } from '@react-native-admob/admob';

const Test = () =>  {
  const {adLoaded, adPresented, show} = useInterstitialAd(
    TestIds.INTERSTITIAL,
    {
      requestOptions: {
        requestNonPersonalizedAdsOnly: true,
      },
    }
  );

  const presentAd = () => {
    console.log("adLoaded: " + adLoaded);
    console.log("adPresented: " + adPresented);

    if(adLoaded) {
      show();
    }

    console.log("adLoaded: " + adLoaded);
    console.log("adPresented: " + adPresented);
  }

  return (
    <View style={styles.body}>
      <Pressable onPress={presentAd}>
        <Text>Press me!</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
   body: {
     flex: 1,
     justifyContent: 'center',
     alignItems: 'center',
   }
});

export default Test;

Package versions

  • React Native: 0.66.4
  • React Native AdMob: ^2.0.0
  • Google Mobile Ads SDK(if specified):
  • Xcode:
  • Java & Gradle: 4.2.2 (gradle)

Affected platforms

  • [ X ] Android
  • [ untested ] iOS
@TimCatana
Copy link
Author

TimCatana commented Jan 5, 2022

It seems that occurs on an emulator but not on a real device. Is this normal behaviour for an emulator? If it is, then is there documentation explaining why this occurs on an emulator but not on a real device?

If this is normal behaviour and documentation for why it occurs exists, then this issue can be marked as closed.

Edit

Now it does not work on my real device. The issue is persistent on both emulator and real device.

@wjaykim
Copy link
Collaborator

wjaykim commented Jan 6, 2022

This is expected behavior. I'm sorry I didn't explained the states' lifecycle within ad.

The hook return values change like this:

  1. Hook is mounted (Ad is created)
  2. Ad is loaded automatically if loadOnMounted option is not false
  3. After ad is loaded adLoaded value is changed to true
  4. According to user's action show() method is fired
  5. After ad is showed adPresented is changed to true (adLoaded value still remains true)
  6. After ad is dismissed by user adDismissed comes to true (adLoaded and adPresented value still remain true)
  7. After new ad is loaded, all states are initialized. (such as adLoaded value is changed to false

As I explained above, the ad states are not initialized until the new ad is loaded.

@TimCatana
Copy link
Author

TimCatana commented Jan 6, 2022

That makes sense. I guess it was a confusion on my end. Thanks for clearing this up for me. I will close this issue but maybe it would be good to add how each variable changes "under the hood" in the documentation so misunderstandings like this don't happen in the future.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants