Blueberry
Blueberry

Reputation: 2231

How to show "up" / "back" button in Xamarin.Forms?

I am trying to work out how to show the "up" arrow in Xamarin.Forms without a pushing a page onto the stack. I.E. I just want to perform an action when the back button is pressed. I am completely stuck on this so any help would be appreciated.

I have tried creating a custom renderer which handles a view property called DisplayHomeAsBack. Which in the renderer calls the following:

FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
  actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}

Unfortunately it seems this does absolutely nothing, even though all online tutorials and stackoverflow question for android suggest this method.

The plan is that I can then use the "OnBackButtonPressed" override in MasterDetailPage, which should allow me to perform this action. Unfortunately displaying the back button has been the larger hurdle so far!

Any idea of a better way to do this or how I can get the current mechanism to work?

EDIT

I have created a project and uploaded it to this question on the Xamarin support forums, if it helps. http://forums.xamarin.com/discussion/comment/186330#Comment_186330

Upvotes: 2

Views: 2060

Answers (2)

Blueberry
Blueberry

Reputation: 2231

I know it sounds like a bit of a hack, but the best "solution" I have found so far is to add a page behind the current page (behind the root) so it is not visible. Then when the user presses the back button, handle it by removing that page.

Upvotes: 0

hvaughan3
hvaughan3

Reputation: 11105

Sorry to keep you waiting so long!

Warning that I did not actually run this code and changed it from my own so I would be surprised if it worked perfectly without some changes.

So below should add a back button where there was not one before (so like when there is not really a page to go back to) and then we will add a custom action to perform when it gets pressed.

I would suggest you push a new page onto the stack without using animation so it is transparent to the user and also makes all of this much simpler, but if you absolutely do not want to do that, the below method should work.

MainActivity:

//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {

    if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever

        Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
        Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")

            ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
        });
    } else {
        Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
    }

    return base.OnCreateOptionsMenu(menu);
}

Now the next step is do do a custom action when it is pressed. I think you can either override OnBackPressed() or OnOptionsItemSelected() in MainActivity or maybe you can override the MasterDetailPage method. I am not sure.

Which ever one works for you, inside of that override, I would simply check to see if you are on your App.AppMasterPage like we did above, and if so, send a MessagingCenter message which your App.AppMasterPage has already subscribed to in order for it to handle the custom action.

If you get stuck let me know!

Upvotes: 1

Related Questions