Search code examples
androidandroid-activityminimizeonpause

How to detect when an Android application is minimized?


How to detect when an Android app goes to the background? onPause() or onUserLeaveHint() works but are also called when the orientation is changed or another activity is presented.


Solution

  • If orientation changes the app will call through the life cycle once again that means from oncreate

    you can avoid it as well by writing the following to code to the manifest

     <activity
          android:name=""
          android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
          android:label="@string/app_name" />
    

    this tell the system that when orientation changes or keyboardHidden or screenLayout changes I will handle it by myself no need to re create it.

    then write your code on on pause