The admob banner loads the first time and is shown , but after I navigate to the other fragment and tap back it doesn't load again. ss
class MainFragment : Fragment() {
private lateinit var adView: AdView
private var initialLayoutComplete = false
private val adSize: AdSize
get() {
val windowMetrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(requireActivity())
val bounds = windowMetrics.bounds
var adWidthPixels = binding.adContainerView.width.toFloat()
// If the ad hasn't been laid out, default to the full screen width.
if (adWidthPixels == 0f) {
adWidthPixels = bounds.width().toFloat()
}
val density = resources.displayMetrics.density
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(requireContext(), adWidth)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMainBinding.inflate(inflater, container, false)
val view = binding.root
Log.d("lifecycleMain","onCreateView main fragment")
recyclerView = binding.recyclerView
recyclerAdapter = RingtonesAdapter(viewModel.ringtoneList, requireContext()) { ringtone ->
val action = MainFragmentDirections.actionMainFragmentToCustomizeFragment(ringtone)
findNavController().navigate(action)
}
recyclerView.adapter = recyclerAdapter
recyclerView.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
loadAdaptiveBanner()
return view
}
private fun loadAdaptiveBanner(){
adView = AdView(requireContext())
binding.adContainerView
.addView(adView)
binding.adContainerView
.viewTreeObserver.addOnGlobalLayoutListener {
if (!initialLayoutComplete) {
initialLayoutComplete = true
loadBanner()
}
}
}
private fun loadBanner() {
adView.adUnitId = getString(R.string.banner_id)
adView.setAdSize(adSize)
val adRequest = AdRequest
.Builder()
.build()
adView.loadAd(adRequest)
}
I'm using the navigation component and each time it navigates to the another fragment it destroys the main fragment it navigated from , so it might be because of that but didn't find how to stop from doing that.but knowing it's my load function are in the onCreateView() the banner should be loaded again .
this is my nav_graph
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.kotlinrings.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main"
>
<action
android:id="@+id/action_mainFragment_to_customizeFragment"
app:destination="@id/customizeFragment"
app:enterAnim="@android:anim/fade_in"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_enter_anim"
app:popExitAnim="@anim/nav_default_exit_anim"
/>
</fragment>
<fragment
android:id="@+id/customizeFragment"
android:name="com.example.kotlinrings.CustomizeFragment"
android:label="fragment_customize"
tools:layout="@layout/fragment_customize"
>
<argument
android:name="RINGTONE"
app:argType="com.example.ring.data.Ringtone" />
</fragment>
</navigation>