AdView causes the Views of other Fragments to be deleted

28 views
Skip to first unread message

Frank King

unread,
Jun 24, 2025, 4:43:32 AM6/24/25
to Google Mobile Ads SDK Developers
Today, I started working with Google Admob to create AdViews for my app, but when I go to the fragment that contains my ad, it causes all of the other fragments to become wiped. Here is the java code for the Fragment that contains the ad:
package com.franksk4.javatutorialapp.ui.Operators;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.franksk4.javatutorialapp.databinding.FragmentOperatorsBinding;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;

import io.noties.markwon.Markwon;

public class OperatorsFragment extends Fragment {

    private FragmentOperatorsBinding binding;
    private Markwon markwon;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        OperatorsViewModel operatorsViewModel = new ViewModelProvider(this).get(OperatorsViewModel.class);
        binding = FragmentOperatorsBinding.inflate(inflater, container, false);

        View root = binding.getRoot();

        final TextView textView = binding.textOperators;

        markwon = Markwon.create(requireContext());

        operatorsViewModel.getText().observe(getViewLifecycleOwner(), markdownString -> {
            if (markdownString != null) {
                markwon.setMarkdown(textView, markdownString);
            } else {
                textView.setText("");
            }
        });

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        MobileAds.initialize(requireContext(), initializationStatus -> {});
        AdView adView = binding.adView;
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

Here is the xml code for the fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.Operators.OperatorsFragment">

    <TextView
        android:id="@+id/text_operators"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="16sp" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-7820825828642163/8896691043"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Thank you!

Mobile Ads SDK Forum Advisor

unread,
Jun 24, 2025, 11:10:50 AM6/24/25
to frank...@gmail.com, google-adm...@googlegroups.com

Hi,

Thank you for contacting the Mobile Ads SDK Support team.

In a Google sample project, I created a fragment and added an AdView to load a banner ad. However, I was not able to replicate the issue related to wiping or removing the fragment.

Could you please provide your sample project for further investigation.

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=500Ht00001rg5E6IAI&entry.80707362=00315515

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.


Thanks,
 
Google Logo Mobile Ads SDK Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-06-24 15:09:58Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01rg5E6:ref" (ADR-00315515)



Reply all
Reply to author
Forward
0 new messages