How to restore the previous Page of the App when the connectivity is gone

Achraf_benalayaAchraf_benalaya USMember ✭✭
edited October 2018 in Xamarin.Forms

I'm developing an application using xamarin forms , I have to check the connectivity , i'm using the connectivity plugin .
So , what I want , if there is no connection , I want to go to no navigation page , and after the navigation is back , I want to go back to the page where I was .
What I was able to do is , to go back to the main page of the application , doing this on my App.xaml.cs .
Is there any way I can go back to the page where Exactly I was ?!

public App()

        if (Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Desktop)
          MainPage = CrossConnectivity.Current.IsConnected ? (Page)new NavigationPage(new ClientPagetablet()) : new NoNetworkPage();
       MainPage = CrossConnectivity.Current.IsConnected ? (Page)new NavigationPage(new ClientPage()) : new NoNetworkPage();


    private  void HandleConnectivityChanged(object sender, ConnectivityChangedEventArgs e)

   Type CurrentPage = this.MainPage.GetType();

        if (e.IsConnected && CurrentPage != typeof(ClientPage))

        this.MainPage = new NavigationPage(new ClientPage());
        if (!e.IsConnected && CurrentPage != typeof(NoNetworkPage))

        this.MainPage = new NavigationPage(new NoNet**workPage());


    protected override void OnStart()
        // if there is no connectivity it's going to go to no network page
        CrossConnectivity.Current.ConnectivityChanged += HandleConnectivityChanged;

Best Answers


  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @AchrafBenalaya You should save the state of current page in OnDisappearing override and restore it if saved state present in OnAppearing of that page.

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    @ShantimohanElchuri is this will work fine on all platforms ?

    can you show me how to do this ?

    Cause On Android, OnDisappearing is called when the user switches to another app on the phone (means when your App goes to the background).

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Do you need two completely different pages? How about updating the appearance of the main page depending on whether there is connectivity.

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    @JamesLavery do you mean I dont have to add a page for no connectivity ? and instead I just call a function to check if there is a connectivity at every page ?
    But I have to show this page cause I have a lottie animation in It and a label for no internet
    else , if you can show me how to do your solution until I found

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    edited October 2018

    I'll put together a quick demo. Are you primarily testing on Android or iOS?

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    I'm testing on Android , iOS and UWP .

    Thank's James !

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Is your Lottie animation shown when there is no internet or when there is internet?

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    I only show the animation page when there is no internet :smile:

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    Thank you James , I'm going to try this right now , just launched the project and I have some issues with android platform to fix right now , I will test and give you feedback asap.
    I have a question , for a small project , that can get bigger in the future and must work on android , ios and uwp , do you recommend using FreshMvvm ?

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    yes It did work fine for me ! this is awesome !
    Thank you @JamesLavery ! You did really help me !
    I created a new project and added files , it works fine
    If you just show me how to apply that on another page (page2 for example )

    Thank you sir !

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Great that it works and has helped. Can you explain what you mean by applying it on another page? Do you mean how to do navigation in FreshMvvm?

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    yes , now I know I have to learn freshMvvm , what I asked you for is , in the example you did , we have one page , what if this page have a button to navigate to another page , and this new page , have the same content of the main page and bind from the model , show it shows the same content .
    lot of explanation . Hope this was clear

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    So you want to have the same 'internet not available' UI on more than one page? Each page having (presumably) its own content.

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    Yes @JamesLavery , let's say we have eventhandler in each page , like connecting on internet , so we have to check on each page .

  • Achraf_benalayaAchraf_benalaya USMember ✭✭

    Thanks @JamesLavery for everything , I guess I have to read it all to understand and keep up with the mvvm !
    Thanks again !

Sign In or Register to comment.