Forum Xamarin Xamarin.Forms

How to avoid PushModalAsync from retrieving variable values from earlier calls?

Hello there, in my Xamarin forms application I have a button that opens another ContentPage by calling “Navigation.PushModalAsync(new Class());” said class contains public variables with default values that get modified during the usage of the application. My issue is whenever the back button is pressed on Android, and the button is re-pressed, instead of the contentpage showing up with its default values, it shows up with the old values from last time it was open, despite me creating a new instance of the class every time PushModalAsync is called. I’ve tried a lot of different things but nothing seems to work.
Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited May 26

    Create a global variable of Page(class) , it would save all the data(variables) inside it .

    1. If you just want to remain the data in Page1

      //Page1
      public Page2 page; 
      public Page1()
      {
          InitializeComponent();
      
          page = new Page2();
      }
      
      
      private void Button_Clicked(object sender, EventArgs e)
      {
          Navigation.PushModalAsync(page);
      }
      
    2. If you want to remain the data during the application , place the global variable inside App class.

         //App
          public Page2 page;
          public App()
          {
              InitializeComponent();
              // instantiate
              page = new Page2();
      
              MainPage = new Page1();
      
          }
      
          //Page1
          private void Button_Clicked(object sender, EventArgs e)
          {
              var page= (App.Current as App).page;
      
              Navigation.PushModalAsync(page);
          }
      

Sign In or Register to comment.