4

I'm trying to implement something similar to OnBackPressed() in a fragment in Xamarin, but the only solutions I found so far are for Java.

Here is one example in Java that does what I want:

OnBackPressedCallback callback = new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            Toast.makeText(mContext, "back pressed", Toast.LENGTH_SHORT).show();
            
            // And when you want to go back based on your condition
            if (yourCondition) {
                this.setEnabled(false);
                requireActivity().onBackPressed();
            }
        }
    };

How could I go about converting that piece of code into C#?

1
  • Please check the updated answer Commented Sep 6, 2022 at 10:08

1 Answer 1

0

You cannot have anonymous classes in C#, so just create a class that inherits this abstract class and you're good:

public class HandleOnBackPressedCallback : OnBackPressedCallback
{
    public HandleOnBackPressedCallback(bool enabled) : base(enabled)
    {
    }

    public override void HandleOnBackPressed()
    {
        Toast.MakeText(context, "back pressed", ToastLength.Long).Show();

        // And when you want to go back based on your condition
        if (yourCondition)
        {
            this.setEnabled(false);
            requireActivity().onBackPressed();
        }
    }
}

Update

Create an interface for handling your event:

public interface IOnBackPressedHandler
{
    bool OnBackPressed();
}

In your activity (below is just an example) setup your interface callback as follows:

public class FragActivity : AppCompatActivity
{

    public override void OnBackPressed()
    {
        Fragment fragment = SupportFragmentManager.FindFragmentById(Resource.Id.yourFragmentId);
        if (!(fragment is IOnBackPressedHandler) || !((IOnBackPressedHandler)fragment).OnBackPressed()) {
            base.OnBackPressed();
        }
    }

}

Finally, your fragment would look something like that:

public class SomeFragment : Fragment, IOnBackPressedHandler
{
    public bool OnBackPressed()
    {
        //Callbacks will be received here.
        return true;
    }
}

You can also make this method a part of your base Fragment mark it as virtual and then have all classes inherit this to keep overriding it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the conversion. How do I actually make the HandleOnBackPressed() method run though? I don't want to call the method directly, what I want is for that method to be called when the user pressed the back button in a fragment. Here is the code I wrote in the fragment: HandleOnBackPressedCallBack callback = new HandleOnBackPressedCallback(true); Activity.OnBackPressedDispatcher.AddCallback(this, callback);
So what you want is that every time your back button is pressed in a fragment you want a callback in your fragment?
I'm not exactly sure how callbacks work. What I'm trying to do is be able to use a specific method when the OnBackPressed() event fires in a specific fragment. So, for example, fragment A would execute method A when OnBackPressed() fires, while fragment B would execute method B when OnBackPressed() fires.
I see updating my answer...

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.