rememberNativeAd

fun rememberNativeAd(adUnitId: String, enabled: Boolean = true, cache: NativeAdCache? = null, cacheKey: Any? = null): NativeAd?(source)

Loads a NativeAd for adUnitId and keeps it alive for as long as it is composed.

One DisposableEffect owns both the request and the destroy, so re-keying destroys the old ad before requesting a new one, and an ad that arrives after disposal is destroyed instead of retained.

The request waits for AdsSdkState.isReady. Initialization of the Mobile Ads SDK is asynchronous and starts during app startup, so a slot composed early would otherwise ask a SDK that is not up yet, which throws, and then never retry. Keying the effect on readiness means the request starts by itself the moment the SDK is up.

Inside a lazy list or grid, pass a cache and cacheKey so the ad outlives its item scrolling out of view; see NativeAdCache.

Return

the loaded ad, or null while loading, after a failure, or when disabled.

Parameters

adUnitId

the ad unit to request; a blank value loads nothing.

enabled

false releases any loaded ad and skips loading, e.g. when the user disabled ads.

cache

keeps the ad while the slot is out of composition. Ignored without cacheKey.

cacheKey

identifies this slot in cache; unique among the slots composed at one time.