Is there any equivalent to NavigationPage from Xamarin.Forms in Xamarin.Android?

atataatata Member ✭✭
edited December 2 in Xamarin.Android

Recently, I switched to Xamarin.Android from Forms. Tried to find any walkthrough to do the same functionality as NavigationPage gives, but either it is not implementable in Android or it is too simple to write about it.
Found only how to override back button or to make navigation drawer. It's not the thing I look for. I need just the same functionality which NavigationPage gives. Actually, I'm not sure even how to formulate the question right.
Could you, please, help me with it?

Tagged:

Best Answer

  • LeonLuLeonLu Xamurai
    Accepted Answer

    Do you want to achieve the page switch from one Activtiy to another activity like following GIF?

    If so, you can try to use StartActivity to open another activity.

     Intent i = new Intent(this, typeof(Activity1));
     StartActivity(i);
    

    In the another Activity, you could achieve the AppCompatActivity. Show he back button, and achieve the back result to override OnOptionsItemSelected .

      [Activity(Label = "Activity1")]
    public class Activity1 : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.layout1);
    
            Android.Support.V7.App.ActionBar actionBar = SupportActionBar;
            if (actionBar != null)
            {
                actionBar.SetHomeButtonEnabled(true);
                actionBar.SetDisplayHomeAsUpEnabled(true);
            }
    
            // Create your application here
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId )
            {
                case Android.Resource.Id.Home:
                    this.Finish(); // back button
                    return true;
            }
    
            return base.OnOptionsItemSelected(item);
        }
    }
    

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    Do you want to achieve the page switch from one Activtiy to another activity like following GIF?

    If so, you can try to use StartActivity to open another activity.

     Intent i = new Intent(this, typeof(Activity1));
     StartActivity(i);
    

    In the another Activity, you could achieve the AppCompatActivity. Show he back button, and achieve the back result to override OnOptionsItemSelected .

      [Activity(Label = "Activity1")]
    public class Activity1 : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.layout1);
    
            Android.Support.V7.App.ActionBar actionBar = SupportActionBar;
            if (actionBar != null)
            {
                actionBar.SetHomeButtonEnabled(true);
                actionBar.SetDisplayHomeAsUpEnabled(true);
            }
    
            // Create your application here
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId )
            {
                case Android.Resource.Id.Home:
                    this.Finish(); // back button
                    return true;
            }
    
            return base.OnOptionsItemSelected(item);
        }
    }
    
  • atataatata Member ✭✭

    It is exactly what I was looking for! That's awesome! Thanks a lot!

Sign In or Register to comment.