djurovicigoor/app-lifecycle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

djurovicigoor/app-lifecycle

Composer 安装命令:

composer require djurovicigoor/app-lifecycle

包简介

App lifecycle state detection plugin for NativePHP Mobile detects foreground/background transitions on Android and iOS.

README 文档

README

App lifecycle state detection for NativePHP Mobile — detects foreground/background transitions on Android and iOS.

Android iOS License

Installation

composer require djurovicigoor/app-lifecycle

Plugin Registration

Register the plugin in your NativeServiceProvider:

// app/Providers/NativeServiceProvider.php

public function plugins(): array
{
    return [
        // ...other plugins
        \Djurovicigoor\AppLifecycle\AppLifecycleServiceProvider::class,
    ];
}

Usage

PHP — Laravel Event Listener

Register a listener in your AppServiceProvider:

// app/Providers/AppServiceProvider.php

use Djurovicigoor\AppLifecycle\Events\AppForegrounded;
use Djurovicigoor\AppLifecycle\Events\AppBackgrounded;
use Illuminate\Support\Facades\Event;

public function boot(): void
{
    // Fires every time the user returns to the app
    Event::listen(AppForegrounded::class, function (AppForegrounded $event) {
        // make an API call, refresh data, etc.
    });

    // Fires every time the user leaves the app
    Event::listen(AppBackgrounded::class, function (AppBackgrounded $event) {
        // flush pending writes, pause timers, etc.
    });
}

Or use a dedicated listener class:

// app/Listeners/SyncOnForeground.php

namespace App\Listeners;

use Djurovicigoor\AppLifecycle\Events\AppForegrounded;
use Illuminate\Support\Facades\Http;

class SyncOnForeground
{
    public function handle(AppForegrounded $event): void
    {

    }
}
// app/Providers/AppServiceProvider.php

Event::listen(AppForegrounded::class, SyncOnForeground::class);

⚠️ NativePHP Mobile has no queue worker. Do not implement ShouldQueue on your listeners — jobs will be written to the database but never processed. Keep listener logic fast and synchronous.

PHP — Livewire Component

Use the #[On] attribute with the native: prefix in any Livewire component:

use Livewire\Attributes\On;
use Livewire\Component;

class Dashboard extends Component
{
    #[On('native:Djurovicigoor\AppLifecycle\Events\AppForegrounded')]
    public function handleForegrounded(int $timestamp): void
    {
        // Refresh data when app returns to foreground
        $this->loadLatestData();
    }

    #[On('native:Djurovicigoor\AppLifecycle\Events\AppBackgrounded')]
    public function handleBackgrounded(int $timestamp): void
    {
        // Save state when app goes to background
        $this->saveCurrentState();
    }
}

JavaScript — Vue 3 (Composition API)

import { onAppForegrounded, onAppBackgrounded } from '@djurovicigoor/app-lifecycle';
import { onMounted, onUnmounted } from 'vue';

export default {
    setup() {
        let stopFg, stopBg;

        onMounted(() => {
            stopFg = onAppForegrounded(({ timestamp }) => {
                console.log('App is active again', new Date(timestamp));
                // fetch fresh data, restart polling, etc.
            });

            stopBg = onAppBackgrounded(({ timestamp }) => {
                console.log('App went to background', new Date(timestamp));
                // pause timers, cancel requests, etc.
            });
        });

        onUnmounted(() => {
            stopFg?.();
            stopBg?.();
        });
    },
};

JavaScript — React

import { onAppForegrounded, onAppBackgrounded } from '@djurovicigoor/app-lifecycle';
import { useEffect } from 'react';

export function Dashboard() {
    useEffect(() => {
        const stopFg = onAppForegrounded(({ timestamp }) => {
            console.log('App foregrounded at', timestamp);
        });

        const stopBg = onAppBackgrounded(({ timestamp }) => {
            console.log('App backgrounded at', timestamp);
        });

        return () => {
            stopFg();
            stopBg();
        };
    }, []);
}

JavaScript — Vanilla / Inertia

import { onAppForegrounded, onAppBackgrounded } from '@djurovicigoor/app-lifecycle';

// Returns an unsubscribe function — call it to clean up
const stopFg = onAppForegrounded(({ timestamp }) => syncData());
const stopBg = onAppBackgrounded(({ timestamp }) => saveState());

// Later, when tearing down:
stopFg();
stopBg();

Events

AppForegrounded

Fired when the user returns the app to the foreground after it was previously backgrounded.

Note: This event does not fire on initial app launch — only on genuine background → foreground transitions.

PHP class: Djurovicigoor\AppLifecycle\Events\AppForegrounded

Property Type Description
$timestamp int Unix timestamp in milliseconds when the transition occurred

AppBackgrounded

Fired when the user leaves the app (presses Home, switches apps, or locks the screen).

PHP class: Djurovicigoor\AppLifecycle\Events\AppBackgrounded

Property Type Description
$timestamp int Unix timestamp in milliseconds when the transition occurred

JavaScript API

import { onAppForegrounded, onAppBackgrounded, Events } from '@djurovicigoor/app-lifecycle';
Function Parameters Returns Description
onAppForegrounded(handler) handler: ({ timestamp: number }) => void () => void Subscribe to foreground transitions
onAppBackgrounded(handler) handler: ({ timestamp: number }) => void () => void Subscribe to background transitions
Events.AppLifecycle.AppForegrounded string PHP class name constant
Events.AppLifecycle.AppBackgrounded string PHP class name constant

Platform Behavior

Android

  • Foreground detection uses NativePHPLifecycle.ON_RESUME
  • Background detection uses NativePHPLifecycle.ON_PAUSE
  • The initial onResume at app launch is suppressed — a wasBackgrounded guard ensures only genuine returns fire the event
  • NativePHP's activity declares android:configChanges="uiMode|colorMode|orientation|screenSize", so screen rotation does not trigger false foreground/background events

iOS

  • Foreground detection uses UIApplication.willEnterForegroundNotification
  • Background detection uses UIApplication.didEnterBackgroundNotification
  • willEnterForeground fires only when returning from the background, not on initial app launch — no extra guard is needed

Plugin Details

Field Value
Author Djurovic Igor
Version 1.0.0
License MIT
Android min SDK 21
iOS min version 15.0
Platforms Android, iOS
NativePHP Mobile ^3.0
PHP ^8.2

Support

For questions or issues, email djurovic.igoor@gmail.com

License

The MIT License (MIT). Please see License File for more information.

djurovicigoor/app-lifecycle 适用场景与选型建议

djurovicigoor/app-lifecycle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 843 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 djurovicigoor/app-lifecycle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 djurovicigoor/app-lifecycle 我们能提供哪些服务?
定制开发 / 二次开发

基于 djurovicigoor/app-lifecycle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 843
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09