Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

PushModalAsync not working correctly after call back activity is fired

spoduguspodugu Member ✭✭
edited October 1 in Xamarin.Forms
  1. Tabbed page is the root and there is "Account" content page as navigation page inside it.
  2. Account page has OnAppearing event.
  3. On a button click in Account page, a modal popup "Login" content page is pushed.
  4. In Login page, user will be authenticated and callback url is called and another modal page, Page1, is pushed. <----Issue is the OnAppearing event of Account page i.e., step 2 is called. It did not get called at step 3.

            var authResult = await WebAuthenticator.AuthenticateAsync(
                new Uri($"mywebsite.com/auth/" + scheme),
                new Uri("myapp://auth#"));
    
            await ((MainPage)Application.Current.MainPage).CurrentPage.Navigation.PushModalAsync(new Page1());
    
  5. Further another issue is when both the modals are closed using PopModalAsync in Page 1, the OnAppearing event of Account page is not called again.

        int count = Application.Current.MainPage.Navigation.ModalStack.Count;
        for (int currModal = 0; currModal < count; currModal++)
        {
            await Application.Current.MainPage.Navigation.PopModalAsync(false);
        }
    
  6. To debug the cause, pushed modal async before and after callback activity, and found that it is causing the issue.

            // OnAppearing of Account page not getting called
            await ((MainPage)Application.Current.MainPage).CurrentPage.Navigation.PushModalAsync(new Page1());
    
            var authResult = await WebAuthenticator.AuthenticateAsync(
                new Uri($"mywebsite.com/auth/" + scheme),
                new Uri("myapp://auth#"));
    
            // ISSUE: OnAppearing of Account page is called here
            await ((MainPage)Application.Current.MainPage).CurrentPage.Navigation.PushModalAsync(new Page1());
    
  7. Below is the callback activity

    [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
    [IntentFilter(new[] { Intent.ActionView },
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "myapp",
    DataHost = "auth")]
    public class WebAuthenticationCallbackActivity : WebAuthenticatorCallbackActivity
    {
    }
    

This behavior is in Android, did not test on iOS.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can try to use MessageCenter to push the Page, You can send the message in the WebAuthenticationCallbackActivity, then subsripte the Message in the on appearing method.

      MessagingCenter.Send<App, string>(App.Current as App, "TwoMessage", currentCount2.ToString());
    
      MessagingCenter.Subscribe<App, string>(App.Current, "OneMessage", (snd, arg) =>
                {
                    Device.BeginInvokeOnMainThread(() => {
                          ((MainPage)Application.Current.MainPage).CurrentPage.Navigation.PushModalAsync(new Page1());
                    });
                });
    
Sign In or Register to comment.