Android: Detect when another Activity is launched (or your activity loses focus)

前端 未结 3 662
鱼传尺愫
鱼传尺愫 2020-12-09 20:43

Like the title says, I need to detect when my app loses focus because another app is launched (Phone call comes in, or user hits Home etc).

Overriding Activity.OnSto

3条回答
  •  萌比男神i
    2020-12-09 21:25

    With ICS upwards this may be possible.

    This is taken from the android site:

    To be notified when the user exits your UI, implement the onTrimMemory() callback in your Activity classes. You should use this method to listen for the TRIM_MEMORY_UI_HIDDEN level, which indicates your UI is now hidden from view and you should free resources that only your UI uses.

    Notice that your app receives the onTrimMemory() callback with TRIM_MEMORY_UI_HIDDEN only when all the UI components of your app process become hidden from the user.

    See this page for full details http://developer.android.com/training/articles/memory.html

提交回复
热议问题