Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Android OnAppearing Not Triggering After PopModalAsync

Is there a work around for Android not triggering after PopModalAsync is called while still using a Modal page. Here is an example:

using System;
using Xamarin.Forms;

namespace XamarinForms
{
    public class OnAppearingBug : ContentPage
    {
        Label label;
        public OnAppearingBug ()
        {
            label = new Label {
                Text = "Page"
            };
            Button button = new Button {
                Text = "PushModal"
            };
            button.Clicked += (sender, e) => Navigation.PushModalAsync (new PopModalBug ());

            Content = new StackLayout {
                Children = { label, button }
            };
        }
        protected override void OnAppearing(){
            label.Text = "OnAppearing";
        }
    }
}

using System;
using Xamarin.Forms;

namespace XamarinForms
{
    public class PopModalBug : ContentPage
    {
        public PopModalBug ()
        {
            Button button = new Button {
                Text = "Pop Back"
            };
            //Bug: This will not trigger OnAppearing on Android
            button.Clicked += (sender, e) => Navigation.PopModalAsync();

            Content = new StackLayout {
                Children = { button }
            };
        }
    }
}
like image 670
dotnetgirl Avatar asked Jul 28 '14 15:07

dotnetgirl


3 Answers

see also http://forums.xamarin.com/discussion/21417/navigationpage-push-pop-event-sequence-different-across-platforms and http://forums.xamarin.com/discussion/18781/ios-and-android-different-behaviors-for-onappearing

but as of the moment (Xamarin.Forms 1.2.2) the only/best solution is the one propose by CrisWebb above:

on your main page - MessagingCenter.Subscribe(this, "popped", (sender) => { // code to run });

on your modal page - MessagingCenter.Send(this, "popped");

like image 95
keith_r Avatar answered Oct 16 '22 14:10

keith_r


This is an issue as indicated by keith_r.

As an alternative to MessagingCenter you can use Xamarin.Forms.Application events triggered when modal page are pushed or popped.

Application events described here.

In your Xamarin.Forms.Application MyApp class you can keep an handle of your OnAppearingBug Page. In MyApp you can listen for modal page pushing/poppig and commanding OnAppearingBug Page as you wish.

like image 2
Mauro Cavallin - Lemcube Avatar answered Oct 16 '22 13:10

Mauro Cavallin - Lemcube


There is a simple work around. For example, my modal page is a Calendar where the user has to select a date before the POP.

So, inside the Modal Page class just create an Event (in my case OnDateSelected) and create a reference from its parent page.

like image 1
Victor Arce Avatar answered Oct 16 '22 12:10

Victor Arce