I am programming a Game using canvas and when the game ends I present an alert dialog in which the player can choose wether to move to the main screen or to restart the game.
The restart option is working but the option where the player can choose to move to the main screen isn't.
I tried using intent but the application doesn't recognize the function StartActivity(intent) when being used in SnakeBoard.
class SnakeBoard : View
{
Context context;
public SnakeBoard(Context context) : base(context)
{
this.context = context;
}
protected override void OnDraw(Canvas canvas)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.SetTitle("Game Over");
builder.SetMessage("what do you want to do next?");
builder.SetCancelable(false);
builder.SetNegativeButton("restart", Restart);//if the user choses restart, Restart method is activated.
builder.SetPositiveButton("go to main menu", MainMenu);//if the user choses go to main menu, MainMenu method is activated.
AlertDialog dialog = builder.Create();
dialog.Show();
}
private void MainMenu(object sender, DialogClickEventArgs e)
{
Intent intent = new Intent(context, typeof(GameActivity));
StartActivity(intent);
}
}
public class MainActivity : AppCompatActivity
{
SnakeBoard sb;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
sb = new SnakeBoard(this);
SetContentView(sb);
}
You should replace the
StartActivity(intent);
with this line
context.StartActivity(intent);
because this class doesn't inheritance from Activity.