0

I am trying to write a program in Android studio and would like to detect OnUserLeaveHint. My code currently works, but it is also called when I click on a button to move to another activity.

How can I make it in such a way that the method is only called when the app is minimized or closed and not when a button is clicked to open another activity?

Selman Tosun
  • 428
  • 5
  • 14
  • 2
    Welcome to SO! Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) showing your current code. This will help the community better understand your question. – jhelphenstine Jun 26 '19 at 00:57

1 Answers1

0

When HOME button is pressed, this is the flow:

onUserLeaveHint()
onPause()
onSavedInstanceState()
onStop()

When transitioning from Activity A to Activity B, this is the flow:

onUserLeaveHint()  (of Activity A)
onPause()  (of Activity A)
onApplyThemeResource  (of Activity B)
onCreate  (of Activity B)
onStart (of Activity B)
onResume  (of Activity B)
onSavedInstanceState()  (of Activity A)
onStop()  (of Activity A)

You need to understand there is no official way to listen to HOME button taps. There are few workaround solutions which seems to work for most of the case but not a 100% concrete solution. What you can do is, use one of the solution to listen to HOME press and turn on a flag. Then use this flag value to differentiate if its a HOME press or Activity change.

Check this for code example: Detect home button press in android

Azhar92
  • 923
  • 2
  • 8
  • 17
  • Thanks alot for you answer you just simplified my problem what I want exactly is how to differentiate between when user leave the app and when an activity changes but how can I create the Flag like you suggested. Thanks in advance – Serenity Emmanuel Jun 26 '19 at 13:19