findNavController()navigate(direction)不适用于Interinitial.setAd



这是onAdClosed或onAdFailed方法中的代码导航不起作用,但在IF语句的其他部分,它起作用

void NavigateFragmentTo(final View v, final int id) {
//Navigation.findNavController(v).navigate(id);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
Navigation.findNavController(v).navigate(id);
Toast.makeText(app, "closed", Toast.LENGTH_SHORT).show();
RequestInterstitial();
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Navigation.findNavController(v).navigate(id);
RequestInterstitial();
}
});
} else {
Navigation.findNavController(v).navigate(id);
}
}

您需要设置从回调完成到尝试导航的延迟。在某些场景中,导航组件似乎还需要一段时间才能真正进行导航。

在下方添加延迟

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
Navigation.findNavController(v).navigate(id);
}, 500);
Toast.makeText(app, "closed", Toast.LENGTH_SHORT).show();
RequestInterstitial();
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
new Handler(Looper.getMainLooper()).postDelayed(() -> {
Navigation.findNavController(v).navigate(id);
}, 500);
RequestInterstitial();
}
});

相关内容

  • 没有找到相关文章

最新更新