adLoaded never changes it's value
TimCatana opened this issue · comments
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
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.
This is expected behavior. I'm sorry I didn't explained the states' lifecycle within ad.
The hook return values change like this:
- Hook is mounted (Ad is created)
- Ad is loaded automatically if
loadOnMounted
option is notfalse
- After ad is loaded
adLoaded
value is changed totrue
- According to user's action
show()
method is fired - After ad is showed
adPresented
is changed totrue
(adLoaded
value still remainstrue
) - After ad is dismissed by user
adDismissed
comes totrue
(adLoaded
andadPresented
value still remaintrue
) - After new ad is loaded, all states are initialized. (such as
adLoaded
value is changed tofalse
As I explained above, the ad states are not initialized until the new ad is loaded.
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.