Calling adMob interstitial ad from another class

Hi, i've got a question regarding Admobs interstitial ads. Is it possible to show an interstial add by calling it from another class?

For example;

Here is the MainActivity.cs

public class MainActivity : Activity
{

    public static InterstitialAd mInterstitialAd;

    protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

        MobileAds.Initialize(this, "AdMob_ID");
        mInterstitialAd = new InterstitialAd(this)
                {
                    AdUnitId = "interstitial_ID"
                };
                AdRequest adRequest = new AdRequest.Builder().Build();
                mInterstitialAd.LoadAd(adRequest);
                mInterstitialAd.AdListener = new AdListener(this);
    }

    public static void ShowAd()
        {

                if (mInterstitialAd.IsLoaded)
                {

                    mInterstitialAd.Show();
                }
        }
}

And then in another class i will simple do
MainActivity.ShowAd();

But that gives me this error: Java.Lang.IllegalStateException: isLoaded must be called on the main UI thread
So i'm wondering if there's another way of showing an interstitial ad?
I'm trying to display them between scenes, so every time i call ReplaceScene in the GameView Director

Best Answers

  • Pengu2Pengu2 ✭✭
    edited August 2018 Accepted Answer

    Nvm, stupid me didn't realize that i need to define an Activity object

    for future use

    public class MainActivity : Activity
    {
    
        public static MainActivity mActivity;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            mActivity = this;
        }
    }
    

    Then in the other class

    MainActivity.mActivity.RunOnUiThread(() =>
    { 
        /*Method*/
    });
    

Answers

  • Pengu2Pengu2 Member ✭✭

    @BillyLiu said:
    @Pengu2

    Please try to use the following code:

    Activity.RunOnUiThread(() => {
       MainActivity.ShowAd();
    });
    

    Hmm i'm not sure if i'm doing anything wrong, but i tried the code you sent in a CCLayer class and i got this error
    An object reference is required for the non-static field, method, or property 'Activity.RunOnUiThread(Action)'

  • Pengu2Pengu2 Member ✭✭
    edited August 2018 Accepted Answer

    Nvm, stupid me didn't realize that i need to define an Activity object

    for future use

    public class MainActivity : Activity
    {
    
        public static MainActivity mActivity;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            mActivity = this;
        }
    }
    

    Then in the other class

    MainActivity.mActivity.RunOnUiThread(() =>
    { 
        /*Method*/
    });
    
  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @Pengu2

    I'm glad your problem is solved. Please mark the solution as the answer, so that can help other community members who have the same issue.
    Thank you for your cooperation.

Sign In or Register to comment.