JohnA
JohnA

Reputation: 636

Disabling back button C# Android Xamarin Code Not Responding

So im trying to disable the back button in my app and it seems like the code im using doesnt want to respond, its hard to explain in words so I made a gif so you can see it more clearly and understand because im not sure I will be able to explain in words.

I want to disable the back button in "Activity2" but the codes I've been trying doesnt want to respond to the back button Here is the GIF

Ive tried these codes, dont know any more solutions since im new to android development.

First attempt

public override void OnBackPressed ()
    {
    base.OnBackPressed ();
    }

Second attempt (Both did the same thing)

public override void OnBackPressed ()
    {
       //  base.OnBackPressed ();            /*  Comment this base call to avoid calling Finish()  */
       //  Do nothing
    }

What could the possible issue be here?

Upvotes: 5

Views: 14134

Answers (5)

Alireza Zainalizadeh
Alireza Zainalizadeh

Reputation: 11

Do Just Like This

public override void OnBackPressed()
{
    return;
}

Upvotes: 0

Uy Suy
Uy Suy

Reputation: 21

You can Try like this
Step 1 Add [Activity(NoHistory =true)
Step 2 Add
protected override bool OnBackButtonPressed() { return true; }
to your content page that you want to disable Back Button Pressed.

Upvotes: -1

ChampChris
ChampChris

Reputation: 1631

I did this and it worked perfectly

 public override void OnBackPressed()
        {
            // This prevents a user from being able to hit the back button and leave the login page.
            return;

            //base.OnBackPressed();
        }

Upvotes: 0

JohnA
JohnA

Reputation: 636

This code will get it to work, make sure you build the program and restart your emulator before you run it (I think thats what made it work for me)

public override void OnBackPressed()
        {
            //  base.OnBackPressed ();            /*  Comment this base call to avoid calling Finish()  */
            //  Do nothing
        }

Upvotes: 0

Jesper Christensen
Jesper Christensen

Reputation: 396

Try this

protected override bool OnBackButtonPressed()
{
    return true;
}

Returning true means that nothing will happen.. if you return false it should still do the default operation (going back)

This is the way to do it on a contentpage atleast... Unsure about activity.

Maybe try this: OnBackPressed in Xamarin Android

protected override void OnBackPressed() 

and

[Activity ( NoHistory = true )]

did you check that pressing the back button actually enters your function?

Set a breakpoint like this : https://i.sstatic.net/7usZI.jpg

And start the application in debug mode (F5)

Upvotes: 13

Related Questions