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!