You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取应用进入后台/锁屏后返回的回调(多Activity场景)

解决方案:监听应用全局前后台切换与锁屏解锁回调

针对多Activity架构下,onPause/onResume会因内部页面跳转误触发的问题,推荐使用Jetpack ProcessLifecycleOwner来监听应用级别的生命周期变化,它能精准捕获「切换到其他应用后返回」「锁屏后解锁」这类全局场景,完全避开内部Activity跳转的干扰。

实现步骤

1. 添加依赖(若未引入)

在模块的build.gradle(或build.gradle.kts)中添加Lifecycle Process依赖:

// Groovy
implementation "androidx.lifecycle:lifecycle-process:2.6.2"
// Kotlin DSL
implementation("androidx.lifecycle:lifecycle-process:2.6.2")

2. 实现应用生命周期观察者

创建一个观察者类,通过DefaultLifecycleObserver监听应用的全局前后台状态:

class AppLifecycleObserver : DefaultLifecycleObserver {
    private var isAppInForeground = false

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        // 仅当应用从后台回到前台时触发(切换应用返回、解锁设备均会触发)
        if (!isAppInForeground) {
            isAppInForeground = true
            // 执行你的业务回调逻辑
            handleAppBackToForeground()
        }
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        // 应用进入后台(切换到其他应用、锁屏时触发)
        isAppInForeground = false
    }

    private fun handleAppBackToForeground() {
        // 这里写你需要的操作,比如刷新数据、恢复状态等
    }
}

3. 在Application中注册观察者

在自定义Application类里完成观察者的注册,确保全局生效:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        // 注册应用生命周期观察者
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver())
    }
}

别忘了在AndroidManifest.xml中指定这个Application:

<application
    android:name=".MyApplication"
    <!-- 其他配置 -->
    >
</application>

进阶:区分「切换应用返回」与「锁屏解锁」

如果需要单独处理解锁场景,可以结合广播监听锁屏状态,在回调中做区分:

class AppLifecycleObserver : DefaultLifecycleObserver {
    private var isAppInForeground = false
    private var wasScreenLocked = false
    private val screenReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (intent?.action) {
                Intent.ACTION_SCREEN_OFF -> wasScreenLocked = true
                Intent.ACTION_SCREEN_ON -> {
                    // 应用在前台时解锁,直接触发解锁回调
                    if (isAppInForeground) {
                        handleScreenUnlocked()
                    }
                }
            }
        }
    }

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        // 注册锁屏广播接收器
        val filter = IntentFilter().apply {
            addAction(Intent.ACTION_SCREEN_OFF)
            addAction(Intent.ACTION_SCREEN_ON)
        }
        ContextCompat.registerReceiver(
            owner as Context,
            screenReceiver,
            filter,
            ContextCompat.RECEIVER_NOT_EXPORTED
        )
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        if (!isAppInForeground) {
            isAppInForeground = true
            if (wasScreenLocked) {
                // 从锁屏解锁回到应用
                handleScreenUnlocked()
                wasScreenLocked = false
            } else {
                // 从其他应用切换回来
                handleAppSwitchedBack()
            }
        }
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        isAppInForeground = false
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        // 注销广播接收器
        owner.unregisterReceiver(screenReceiver)
    }

    private fun handleAppSwitchedBack() {
        // 切换应用返回的业务逻辑
    }

    private fun handleScreenUnlocked() {
        // 解锁设备的业务逻辑
    }
}

方案优势

  • 完全适配多Activity架构,内部页面跳转不会触发误回调
  • 无需在每个Activity中重复实现逻辑,全局统一管理
  • 基于Jetpack官方组件,稳定性和兼容性有保障

内容的提问来源于stack exchange,提问作者ssindher11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:53:17