3

I need to figure out why my Activity's onPause/onStop is called, specifically, whether another Activity (especially from another app) has replaced it, or not.

I can think of a few reasons why my Activity may be paused/stopped, as well as a few Activity life cycle methods (onUserLeaveHint, onBackPressed , onPause, onStop, onDestroy, isFinishing) that may help me in differentiating them, but so far I haven't been able to complete it. Here they are:

Turn off display:
onPause - isFinishing FALSE
onStop - isFinishing FALSE

Back button:
onBackPressed - isFinishing TRUE
onPause - isFinishing TRUE
onStop - isFinishing TRUE
onDestroy - isFinishing TRUE

Home button:
onUserLeaveHint - isFinishing FALSE
onPause - isFinishing FALSE
onStop - isFinishing FALSE

Task switcher:
onUserLeaveHint - isFinishing FALSE
onPause - isFinishing FALSE
onStop - isFinishing FALSE

New Activity:
onUserLeaveHint - isFinishing FALSE
onPause - isFinishing FALSE
onStop - isFinishing FALSE

Notably missing is when Android low memory killer kills my Activity. I haven't been able to reproduce that yet hence no results.

So, I'm able to differentiate when the back button is pressed, and when the display is turned off. However, pressing the home button, task switcher and launching a new activity are identical programmatically to me. How do I differentiate between them?

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • Find the Android Life Cycle http://www.codeproject.com/Tips/648480/Android-Activity-Life-Cycle – Sathish Kumar J Jul 07 '16 at 08:01
  • You can catch the home button in the `onKeyDown()` – Shark Jul 07 '16 at 08:36
  • onKeyDown doesn't detect the home button. – user1118764 Jul 07 '16 at 09:09
  • Why am I being downvoted? – user1118764 Jul 08 '16 at 01:07
  • I don't know why this would be downvoted. Perhaps it is that people assume you have no valid need to differentiate between these situations, that your app shouldn't care. Well I have a need to know (to mitigate an Android bug regarding dismissal of Presentation objects), which is how I ended up here.. Hope my upvote helps! Another reason to add to your list: orientation change. – Jeremy Frank Feb 09 '17 at 12:19

1 Answers1

0

OK I ended up doing a few things to differentiate the various cases:

Set a flag in onBackPressed() to detect the back button Check the new foreground app in the service to detect the launcher and task switcher. In all other cases of a new foreground app, inform the user.

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • Check the new foreground app in the service to detect the launcher and task switcher ? Can you elaborate ? – Renetik May 04 '19 at 06:21