L
L
LoliDeveloper2020-09-13 10:45:33
C++ / C#
LoliDeveloper, 2020-09-13 10:45:33

Why is OnBackPressed not working correctly?

In MainActivity.cs :

intent = new Intent(this, typeof(LevelActivity));
StartActivity(intent);


In LevelActivity.cs :
public void OnClick(View v)
        {
            switch (v.Id)
            {
                 case Resource.Id.backButtonLevels:
                 this.OnBackPressed();
                 break;
            }
        }


It exits , but each subsequent time, when I again click on the "start" in the MainActivity, then the "Back" button must be pressed not 1, but 2 times, then 3, then 4, and so on, so that it would come out.
I tried to find out how much the maximum, reached 14 and stopped checking.
That is, 14 times it was necessary to press the "Back" button so that it returned to the main screen.
With each click of the "Start" button, the number of required clicks on the "Back" button increased by 1.

I need that every time the user enters the LevelActivity window, he needs to click only 1 time on the "Back" button, regardless of how many times he went between the main page and the page with levels

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LoliDeveloper, 2020-09-13
@LoliDeveloper

SO, the thing is, when you return to the page like this, the OnStart() function is called, and my OnStart() was like this:

protected override void OnStart()
{
      base.OnStart();
      startButton.Click += StartButton_Click;
}

As you can see, I subscribed to the event every time I went to this page.
The problem was solved by moving the subscription from the OnStart() method to the OnCreate() method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question