Home > Back-end >  Android onStop gets called right after tap home button
Android onStop gets called right after tap home button

Time:12-20

I have a quick question regarding a basic feature of Android. It's clear that onPause is called when the screen is partially visible. Further onStop is supposedly called only when the screen is no longer in either the foreground or background.

Strangely however when I press the square home button on my phone onStop is called each time in my activity. Even though the activity screen is partially visible I see a log after 0.5 - 1 second of showing in the background. This causes onRestart to be called when returning from a simple pause. Since I am attempting to show an ad here this is slightly problematic. I tested on my Samsung Galaxy A51 and Pixel 3 XL API 30 emulator with the same result.

Currently I am wondering why this method is called here and would like to fix my understanding. I could very well be missing something obvious and apologize if so (low memory condition?). Below are screenshots with the system log and activity lifecycle diagram. I show before any taps, with the log window after 1 tap and then the log window after returning and tapping a 2nd time.

enter image description here

enter image description here

No taps

1 tap square button

2 taps square button

Diagram

CodePudding user response:

What is this pause button? If you mean it power button or home button, there should never be a hardware button directly connected Activity's lifecycle onPause.

When you press a power button or home button, it goes lock screen or Recents screen, in either of these scenarios your app is certainly stopped (onPause then onStop) rather than just suspended (only onPause).

It seems that going directly onResume from onPause may be rather rare case.

onPause()

  • In Android 7.0 (API level 24) or higher, multiple apps run in multi-window mode. Because only one of the apps (windows) has focus at any time, the system pauses all of the other apps.
  • A new, semi-transparent activity (such as a dialog) opens. As long as the activity is still partially visible but not in focus, it remains paused.

About the latter scenario, you can find an answer on StackOverflow.

So, if your app isn't coded to cause a direct onPause to onResume lifecycle, your app should go onStop right after onPause as a normal behavior.

  • Related