'I'm really tired , Interstitial ad show after the the fragment loaded

i have Activity MainActivity and use some Fragment. i try the code in google developer and put the code ad in onAttach and onCreate but Interstitial ad show after the the fragment loaded i worked in the past for ad with activity it's very easy , but it's first time to use ad in fragment class and i search in google search and youtube and no't found any thing help me. so I decided to ask the experts here. I wish you a good day.

This is the path that I want the ad to appear before loading the Fragment Class and when the Fragment Class  is finished and loading Home Fragment

public class MainActivity extends AppCompatActivity {
NavController navController ;
NavHostFragment navHostFragment;

AdView adView;
FrameLayout adContainerView;

public static InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





    BottomNavigationView bottomNavigationView =  findViewById(R.id.bottomNavigationView);
    navHostFragment = (NavHostFragment) getSupportFragmentManager()
            .findFragmentById(R.id.nav_host_fragment);

    if (navHostFragment != null) {
        navController = navHostFragment.getNavController();
    }

    NavigationUI.setupWithNavController(bottomNavigationView, navController);



}

The Fragment i want to show ad befor loding and onBackPressed Fragment before back to MainActivity

public class Report_Fragment extends Fragment  {
View view;
NavController navController;

DatabaseAccess dp;
int bundle_mor, bundle_even;
int dp_count_mor, dp_count_even;
int total_az;

boolean statusAnimation = false;
Handler handlerAnimation = new Handler(Looper.getMainLooper());
ImageView imgAnimation1, imgAnimation2;
Button button;

public   static InterstitialAd mInterstitialAd;

private static final String TAG = "##Report_Fragment##";

public Report_Fragment() {
    // Required empty public constructor
}

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    //////*******////
    //Ads
    //Interstitial
    MobileAds.initialize(getActivity(), initializationStatus -> {});

    AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(getActivity(),(getResources().getString(R.string.Interstitial_id_Forward)),

            adRequest,
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    // The mInterstitialAd reference will be null until
                    // an ad is loaded.
                    mInterstitialAd = interstitialAd;
                    Log.i(TAG, "onAdLoaded");
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    // Handle the error
                    Log.i(TAG, loadAdError.getMessage());
                    mInterstitialAd = null;
                }
            });
    ///////////////**************////////////////
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    showAds();




}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.report_fragment, container, false);


    TextView tv_az_count = view.findViewById(R.id.count_athkar);
    TextView tv_total_dp = view.findViewById(R.id.count_total);

    button = view.findViewById(R.id.button_text);
    imgAnimation1 = view.findViewById(R.id.imgAnimation1);
    imgAnimation2 = view.findViewById(R.id.imgAnimation2);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (getActivity() != null)
                showAds();
            getActivity().onBackPressed();

        }
    });


    dp = DatabaseAccess.getInstance(getActivity());
    dp.open();


    Bundle bundle = getArguments();

    if (bundle != null) {
        bundle_mor = bundle.getInt("bundle_morning");
        bundle_even = bundle.getInt("bundle_evening");
    }

    if (bundle_mor > 0) {
        tv_az_count.setText(String.valueOf(bundle_mor));
        dp_count_mor = dp.getCount_mor();
        total_az = bundle_mor + dp_count_mor;
        dp.update_n(new Items(1, total_az));
        dp.close();
        tv_total_dp.setText(String.valueOf(total_az));
    } else if (bundle_even > 0) {
        tv_az_count.setText(String.valueOf(bundle_even));
        dp_count_even = dp.getCount_ev();
        total_az = bundle_even + dp_count_even;
        dp.update_even(new Items(1, total_az));
        dp.close();
        tv_total_dp.setText(String.valueOf(total_az));
    }

    return view;
}

@Override
public void onDestroy() {
    if (getActivity() != null) {
        BottomNavigationView bottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setVisibility(View.VISIBLE);
           Toast.makeText(getContext(), "Destroy_Fragment", Toast.LENGTH_SHORT).show();
    }
    super.onDestroy();
}




private void showAds() {
    if (mInterstitialAd != null) {
        mInterstitialAd.show(getActivity());
    } else {
        Log.d("TAG", "The interstitial ad wasn't ready yet.");
    }
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source