How not to reload CollectionView after clicking on Back button

Alice 61 Reputation points
2021-04-09T21:09:50.85+00:00

Hi I'm facing a problem with my small xamarin app.

I have my small xamarin-form app that everytime the user enter a product Id click OK button retrieves a single (data related to that product Id only) data thru Web API from page A, and then pass it to a collectionview in another page B.

then there is a back button to go back to page A and the user must enter another product Id again and the app must fetch data thru web api again and pass it again to page B in the collectionview.

So the problem is now, when I click on back button in Page B, it's clear the collectionview data, whereas I need the collectionview to keep the state even If I pop back.

I'm not using any MVVM pattern or complicated stuff from now as I'm new to xamarin; it's just a simple architecture.

Or maybe I'm using wrong technics to achieve this and can advise me a better way to achieve that. I'm please open to your suggestion !!!

In brief, I need to be able not to reload CollectionView after clicking on Back button.

Below is the code that Bind to the View on Page B

public async void OnGetProductDetailButton(object sender, EventArgs e)
{

await Navigation.PushAsync(new ProductDetailPage
{
    BindingContext = productDetails
});

}

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-04-12T02:55:01.567+00:00

    Hello,

    Welcome to Microsoft Q&A!

    We could create a global variable to store the data from Web API in page A ,and pass the data when pushing to page B .

       private ProductDetail productDetails ;  
         
       public async void OnGetProductDetailButton(object sender, EventArgs e)  
       {  
         
           if(productDetails == null){  
                //call web api  
               // productDetails  = data;   
         
           }  
           await Navigation.PushAsync(new ProductDetailPage  
           {  
               BindingContext = productDetails  
           });  
       }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments