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

Problem with popping modal on Android.

$
0
0

In my MainActivity, I'm starting the application and provide a RootPage. This goes as follows:

     public class MainActivity : XFormsApplicationDroid
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);


                if (!Resolver.IsSet)
                {
                    this.SetIoc();
                }
                else
                {
                    var app = Resolver.Resolve<IXFormsApp>() as IXFormsApp<XFormsApplicationDroid>;
                    app.AppContext = this;
                }


                Xamarin.Forms.Forms.Init(this, bundle);


                SetPage(BuildView());

            }

            static Page BuildView()
            {
                return new RootPage();
            }
    }

In the constructor of the RootPage, it checks if the user is authenticated. If not, then a modal appears.

    public class RootPage : MasterDetailPage
        {

            OptionItem previousItem;

            public RootPage()
            {

                var optionsPage = new MenuPage{ Title = "menu"};

                optionsPage.Menu.ItemSelected += (sender, e) => NavigateTo(e.SelectedItem as OptionItem);

                Master = optionsPage;

                NavigateTo(optionsPage.Menu.ItemsSource.Cast<OptionItem>().First());


                if (!App.Instance.IsAuthenticated)
                {
                    ShowLoginDialog();
                }

            }

            async void ShowLoginDialog()
            {
                var page = new LoginPage();

                await Navigation.PushModalAsync(page);
            }
    }

The modal is just an empty ContentPage in my shared project.

    public class LoginPage : ContentPage {}

I implemented a custom renderer on the Android project, which checks if the user is authenticated and if they are, the accesstoken is saved.

    [assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))]

    namespace KnowNow.Droid.Renderers
    {
        public class LoginPageRenderer : PageRenderer
        {

            protected override void OnElementChanged (ElementChangedEventArgs<Page> e)
            {
                base.OnElementChanged (e);
                App.Instance.SaveToken("TEST");
            }


        }
    }

    // This is the relevant code in App.cs
    public bool IsAuthenticated
    {
        get { return !string.IsNullOrWhiteSpace(_Token); }
    }

    string _Token;
    public string Token
    {
        get { return _Token; }
    }

    public void SaveToken(string token)
    {
        _Token = token;
        Navigation.PopModalAsync();
    }

My problem is, that it always shows this unhandled exception (see call stack below). And all it says in application output is this:

11-07 10:46:41.882 I/dalvikvm( 1856): threadid=3: reacting to signal 3
11-07 10:46:41.882 I/dalvikvm( 1856): Wrote stack traces to '/data/anr/traces.txt'
11-07 10:46:42.391 I/dalvikvm( 1856): threadid=3: reacting to signal 3
11-07 10:46:42.391 I/dalvikvm( 1856): Wrote stack traces to '/data/anr/traces.txt'
11-07 10:46:42.891 I/dalvikvm( 1856): threadid=3: reacting to signal 3
11-07 10:46:42.891 I/dalvikvm( 1856): Wrote stack traces to '/data/anr/traces.txt'
11-07 10:46:43.392 I/dalvikvm( 1856): threadid=3: reacting to signal 3
11-07 10:46:43.392 I/dalvikvm( 1856): Wrote stack traces to '/data/anr/traces.txt'
11-07 10:46:43.901 I/dalvikvm( 1856): threadid=3: reacting to signal 3
11-07 10:46:43.901 I/dalvikvm( 1856): Wrote stack traces to '/data/anr/traces.txt'
11-07 10:46:44.222 D/Mono ( 1856): DllImport attempting to load: '/system/lib/liblog.so'.
11-07 10:46:44.222 D/Mono ( 1856): DllImport loaded library '/system/lib/liblog.so'.
11-07 10:46:44.222 D/Mono ( 1856): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-07 10:46:44.222 D/Mono ( 1856): Searching for '__android_log_print'.
11-07 10:46:44.222 D/Mono ( 1856): Probing '__android_log_print'.
An unhandled exception occured.

I suppose this has something to with the order I'm doing this, but I can't seem to figure out why this is happening.

()


Viewing all articles
Browse latest Browse all 58056

Trending Articles