3

I have an Android Activity that extends AppCompatActivity and I am getting very confused because when I test it on my phone ( a Sony Xperia on Android 7.1.1, API 25 ) the onPause method is not called when the Home button is pressed.

In fact it appears as though no Activity Lifecycle events are triggered by the app being put in the background. I have the following in my Activity:

@Override
public void onPause()
{
    super.onPause();
    Log.i(TAG, "Pause the Activity.");
}

@Override
public void onStop() {
    super.onStop();
    Log.i(TAG, "Stop the Activity");
    forceClose();
}

@Override
protected void onUserLeaveHint(){
    super.onUserLeaveHint();
    Log.i(TAG, "User leaving the Activity");
    forceClose();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (!hasFocus)
    {
        Log.i(TAG, "Window focus changed- Activity no longer has focus.");
    }
}

When I hit the "Home" button, none of these are called. In fact there doesn't seem to be anything happening in my code, my app just vanishes from view. If I start a different app, all of the Activity closing lifecycle calls - including onPause - suddenly happen at that point.

This is a problem for me because the activity is fairly battery intensive and I don't want to waste the user's battery if they have put it in the background. Also it doesn't seem to recover well from being backgrounded, so it seems easier to cancel it.

There are various questions about this on SO ( this might be closest ) but none of them seem to cover this scenario - the onPause documentation suggests that it is called when an app is sent to the background, but in other places I have seen it suggested that it is only called when another app is launched, so possibly that explains what I am seeing.

How can I find when my app has been sent to the background?

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • try to show Toast once in onPause method and see what happens. – Suraj Vaishnav Jun 05 '18 at 16:58
  • @SurajVaishnav I have it writing to Logcat and debug breakpoints set up, it's unambiguous as to when it is and isn't being called. Being backgrounded never triggers it. – glenatron Jun 05 '18 at 16:59
  • maybe after app closes, debugger stops sending logs from the device. just a thought – SafalFrom2050 Jun 05 '18 at 17:08
  • @SafalSharma nope, it's still right there in the background popping notes to Logcat the whole time. It doesn't stop until a different app is opened. Then `onPause` gets called. Not when _my_ app is paused. – glenatron Jun 05 '18 at 17:10
  • Try to override other methods like onRestart(), onResume(), and onDestroy(). After that, instead of clicking the home button, try the back button and see if any method or log message is triggered. – Red M Jun 05 '18 at 17:31
  • It seems that writing to Logcat happens in batches, so one can't tell exactly from the log when something "really" happened (at least that's what I'm experiencing with devices by some other manufacturer). Could you make sure of the "real" time by adding a timestamp of your own to the log message? Having said that, your observations may be 100% correct and the delay in calling the lifecycle methods may be intended: if the user only briefly visits the home screen and then returns to your app, it may actually save resources (energy) to not make an app prepare for entering the saved state. – Bö macht Blau Jun 05 '18 at 18:42
  • @0X0nosugar I figure that regardess of Logcat, debugger breakpoints probably happen at approximately the right time and they are being hit at the same time as the logging. The problem I have is that it means there appears to be no way to tell the activity is no longer visible when the user has hit "home" and that it doesn't need to be picking up loads of battery-intensive sensor feedback. – glenatron Jun 05 '18 at 20:09

0 Answers0