Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

One solution for Initial Dialog Challenge

$
0
0

There is a repeating issue on the forums about how to have an initial dialog show without it being pushed into the history or having the user be able to use the hardware back button to get to the main page.

The solution that has worked for me is to modify the platform specific startup code:
In the following code snippets, App.Initialize() is setting up some database tables and App.IsRegistered checks for a specific entry in one of those tables.
I use an observable for notifications for a few reasons: They are far more flexible that the messaging system, and I can guarantee the synchronization context (thread) that the subscription code is executed on. That is simply my opinion use whatever signaling mechanism works best for you.

This code gives me exactly what I want. A user registration screen that cannot be bypassed and does not show up in the history. Your mileage may vary.

MainActivity.cs:

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


            Forms.Init(this, bundle);
            App.Initialize();
            RegistrationService.RegistrationStateObservable.ObserveOn(SynchronizationContext.Current)
                               .Subscribe(x =>
                                   {                                       
                                       if(x == RegistrationState.Registered)
                                           SetPage(new NavigationPage(new HomePage()));
                                       if (x == RegistrationState.Revoked)
                                       {
                                           App.ClearLocalData();
                                           SetPage(new Revocation());

                                       }
                                   });


            if(App.IsRegisterd)
                SetPage(new RegisterPage());
            else
                SetPage(new NavigationPage(new HomePage()));
        }

AppDelegate.cs (I added a SetPage function for convenience)

           public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Forms.Init();
            App.Initialize();

            window = new UIWindow(UIScreen.MainScreen.Bounds);
            RegistrationService.RegistrationStateObservable.ObserveOn(SynchronizationContext.Current)
                             .Subscribe(x =>
                             {
                                 if (x == RegistrationState.Registered)
                                     SetPage(new NavigationPage(new HomePage()));
                                 if (x == RegistrationState.Revoked)
                                 {
                                     App.ClearLocalData();
                                     SetPage(new Revocation());
                                 }

                             });

            if (App.IsRegisterd)
                SetPage(new NavigationPage(new HomePage()));
            else
                SetPage(new RegisterPage());

            window.MakeKeyAndVisible();

            return true;
        }

        private void SetPage(Page page) { window.RootViewController = page.CreateViewController(); }

I don't know about windows phone but I'm sure the startup code could be similarly modified.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>