JohnA
JohnA

Reputation: 594

How to disable the back button in android C#

Im making an Android App using Xamarin.Android in Visual Studio and I've been trying to disable the back button in my secondary activity, and the methods I've been using is adding this code into the activity

@Override
public void onBackPressed() {
}

So the code would look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;

namespace The_Coder_Quiz
{
    [Activity(Label = "Activity2")]
    public class Activity2 : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.easyQuiz);

            // Create your application here

        }

        @Override
public void onBackPressed()
        {
            //Include the code here
            return;
        }

    }
}

But that gave me an error which was:

member modifier 'public' must precede the member type and name

Im assuming that is because im doing the project in C#, what would be the correct solution since im doing this in C# and not Java (new to android development)

Im trying to keep off StackOverflow when learning but its kinda hard to find references online for Android Development in C#

Upvotes: 1

Views: 2462

Answers (2)

JanR
JanR

Reputation: 6132

The correct way to do an override in C# is:

  public override void OnBackPressed()
  {
        //Include the code here
        return;
  }

Upvotes: 2

IbrahimSahab
IbrahimSahab

Reputation: 194

For example if it is a login page (Activity):

public override void OnBackPressed(){
 //by this way.. user want be able to hit the back button 
 // unless user is logged in

     if (user.IsLoggedIn()){
         base.OnBackPressed();
      }
}

Check this: OnBackPressed in Xamarin Android

And about android development using Xamarin, these are some tutorials:

Upvotes: 0

Related Questions