locked
OnPause() not calling RRS feed

  • Question

  • User2405 posted

    I have an activity in which I want to run some code when the activity goes out of view (such as pressing the back button to go to the previous activity, locking the screen, or pressing the home button). I have tried putting in OnPause() into my code, but it doesn't work. I have put a breakpoint there in debug mode but that breakpoint is never triggered. I am implementing it like:

    protected override void OnPause()
            {
                base.OnPause();
                //Execute code here
    
            }
    

    but it doesn't ever seem to enter this code. I assume there is something simple I am missing, but I cannot find the answer.

    Any help is much appreciated.

    Wednesday, September 6, 2017 7:48 AM

Answers

  • User92365 posted

    Hello @Metallor

    i'm not an expert but i've create a new android applicatin with visual studio 2015 add a second activity with a stupid layout just a button.

    on both activity i override onPause method , add a breakpoint and each time i launch an activity i pass on the onPause method or if i click on back ...

    no idea about your problem ,but just a question the onPause method it is a copy paste OR you type Override ...the autocompletion displays add a list , you choose and click enter ?

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, September 6, 2017 9:38 AM

All replies

  • User92365 posted

    Hello @Metallor

    i'm not an expert but i've create a new android applicatin with visual studio 2015 add a second activity with a stupid layout just a button.

    on both activity i override onPause method , add a breakpoint and each time i launch an activity i pass on the onPause method or if i click on back ...

    no idea about your problem ,but just a question the onPause method it is a copy paste OR you type Override ...the autocompletion displays add a list , you choose and click enter ?

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, September 6, 2017 9:38 AM
  • User2405 posted

    Turns out I was adding this code to the incorrect activity. OnPause does call after all, thank you for your reply. Having you reply made me try calling OnPause in other activities, and when that did work I ended up adding it to the correct activity. So thank you for making me realise what was wrong.

    Wednesday, September 6, 2017 10:07 AM
  • User364990 posted

    What do you mean calling OnPause "in other activity"? Shouldn't it be in MainActivity? I'm having the same problem. OnCreate(), OnDestroy(), OnResume() all work. But OnPause() never fires. Unfortunately neither OnDestroy() or SurfaceDestroy() get called when screen goes to sleep. So there's nothing triggered to stop the app from processing the camera.

    Sunday, February 4, 2018 2:39 AM
  • User2405 posted

    OnPause() can be called from any activity, it doesn't necessarily have to be the MainActivity. So you just need to call it from whichever activity you are using the camera from.

    Sunday, February 4, 2018 12:09 PM