rotorgames / Rg.Plugins.Popup

Xamarin Forms popup plugin
MIT License
1.15k stars 337 forks source link

Initialize Rg inside other activity (splash screen) #714

Open Cvijo opened 2 years ago

Cvijo commented 2 years ago

💬 Questions and Help

Hi, In Xamarin I am using splash screen to play some Lottie animation and during animation, I am initializing all libraries (even creating a new App())

So my Splash screen inherits Activity, IAnimatorListener when my Lottie animation start I begin registering all libraries in background thread

part of code in my SplashActivity


    [Activity(Label = "MyApp", Theme = "@style/MyApp.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : Activity, IAnimatorListener
    {
. 
.
.
        public async void OnAnimationStart(Animator animation)
        {
            if (IsInitializing == false && MainActivity.OPTIMIZE_STARTUP)
            {
                await Task.Run(() => Init(this, savedInstanceState))
                    .ContinueWith(app => IsLoaded = true);
            }
        }

        private App Init(Activity context, Bundle savedInstanceState)
        {
            Forms.Init(context, savedInstanceState);
            Xamarin.Essentials.Platform.Init(context, savedInstanceState);
            FormsMaterial.Init(context, savedInstanceState);
            OneSignal.Current.StartInit(API_KEY)
                 .InFocusDisplaying(OSInFocusDisplayOption.Notification)
                 .EndInit();
            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);
            Rg.Plugins.Popup.Popup.Init(context);  //-- this doesn't work if not in main activity
            MainActivity.AppInstance = new App();
            return MainActivity.AppInstance;
        }
 }

everything works fine except this line Rg.Plugins.Popup.Popup.Init(context); if I move it to main activity it's working I am just wondering why .. it's no problem to put it in the main activity it's just a few ms initializations but maybe I am doing something wrong here?

I even try to use Application.Contex instead of this, tried to inherit FormsAppCompatActivity instead of Activity and many other things but only init() in main activity working

LuckyDucko commented 2 years ago

Hmm, what error do you get from this? As long as what you pass is of type Android.Content.Context it should be functional

Cvijo commented 2 years ago

There is no error, not in debug window or anywhere it just doesn't work. Popup doesn't show, if I put it in my main activity it works. And yes I am passing "this" from splash activity

LuckyDucko commented 2 years ago

Huh, could you try and remake what you're doing in a demo project, zip and put it here, and i can go through and poke about?

Cvijo commented 2 years ago

Hi, here is a simplified version of the demo, when you init Rg on another thread or on another activity it doesn't work, if you uncomment the first line it works.

I remove most of the things and simplified activity but it reproduces issues with Rg .. navigation stacks are still counting but the popup doesn't show

Rg_demo.zip

RonnyBansemer commented 2 years ago

I'm facing the same issue. If somebody is using Prism.Plugin.Popups it is required to call Rg.Plugins.Popup.Popup.Init(this); in SplashActivity (before Forms init) and again in MainActivity. Simply moving it from SplashActivity to MainActivity does not solve the problem. Affected version: 2.0.0.14