0

Recently i decided to give Xamarin Forms a try, so i started migrating my Windows Phone / Android Xamarin project, to the new "Forms" format.

After migrating two simple screens (views), the code runs just fine on my Windows Phone, but it crashes on the Android Platform. I am using two devices to test the code, with no emulator to avoid unecessary headaches.

The exception raised on the android platform is: System.Exception: Android only allows one navigation page on screen at a time.

The problem is not related to the "View" class itself, since it doesn`t happen once i set it as the "start screen" for the Android.

I'm afraid the problem is related to the fact that i am "Pushing" two screens via the "Navigation.PushAsync(...)" method provided by Xamarin.

Any ideas on how do actually fix it ?

EDIT:

Code Sample

Overview: The "MainView.cs" is just a content page with a label on the top, followed by two buttons alligned vertically.

How to reproduce the exception: Click on "Get All Users" button on the mainview. I have modified the code to push the same view once the button is clicked, instead of pushing another view (The view that has a listview of users, which i haven't included, just to make it simple). Once the button is clicked, it will navigate to a new instance of "MainView" by calling "Navigation.PushAsync(...)". This will trigger the exception after the screen is loaded.

3
  • Please post the code that is causing the exception. Commented Jun 14, 2014 at 16:52
  • Added a code sample with some minor overview of the exception and how to reproduce it. Let me know if you need anything else. Commented Jun 14, 2014 at 17:06
  • Please post code inline, properly formatted, instead of using an external link. Commented Jun 14, 2014 at 17:25

1 Answer 1

3

You are nesting Navigation pages. This is not necessary. Try this instead:

public class ViewsHandler
{
        public static Page GetMainPage()
        {
            return new MainView();
        }

        public static Page GetUsersListPage()
        {
            return new UsersListView ();
        }
}

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

            // Initializing Xamarin Form
            Xamarin.Forms.Forms.Init (this, bundle);

            // create a single NavigationPage wrapping your content
            SetPage (new NavigationPage(ViewsHandler.GetMainPage()));
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Solved my problem. Would you mind explaining a little bit more, WHY does it work?
In your original code, you were pushing a NavigationPage onto an already existing navigation stack. You only need one NavigationPage in a hierarchy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.