Git Product home page Git Product logo

douglasjunior / react-native-applifecycle Goto Github PK

View Code? Open in Web Editor NEW
33.0 1.0 2.0 429 KB

⚛ Enhances the reliability of managing app lifecycles across iOS and Android platforms, ensuring consistent behavior regarding foreground and background states.

Home Page: https://www.npmjs.com/package/react-native-applifecycle

License: MIT License

JavaScript 8.32% TypeScript 53.28% Java 38.40%
appstate lifecycle react-native android ios

react-native-applifecycle's Introduction

React-Native App Lifecycle

License MIT npm version npm downloads

⚛ Enhances the reliability of managing app lifecycles across iOS and Android platforms, ensuring consistent behavior regarding foreground and background states.

It implements the Lifecycle for Android and falls back to the AppState for iOS.

Why Use This?

The original AppState API provided by React Native behaves differently between Android and iOS, particularly regarding the background state:

  • On iOS, the background state signifies that the entire app is in the background.
  • On Android, the background state indicates that the React Native Activity is in the background, which might not necessarily mean the entire app is in the background.

By using react-native-applifecycle, you can handle these differences seamlessly across both platforms, ensuring that the state background will be dispatched only when the entire app is in background.

Install

Install dependency package

yarn add react-native-applifecycle

Or

npm i -S react-native-applifecycle

Usage

The App Lifecycle API is compatible with the original AppState.

Subscribing to the change listener:

import {AppLifecycle} from 'react-native-applifecycle';

const App = () => {

  useEffect(() => {
    const listener = AppLifecycle.addEventListener('change', state => {
      // do something
    });

    return () => listener.remove();
  }, []);

  return <View />;
}

export default App;

Getting the current state with hook:

import {useAppLifecycle} from 'react-native-applifecycle';

const App = () => {
  const currentLifecycle = useAppLifecycle();

  // do something

  return <View />;
}

export default App;

For more details, see the Sample Project.

Lifecycle States

  • active - The app is running in the foreground
  • background - The app is running in the background. The user is either:
    • in another app
    • on the home screen
  • [iOS] inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the multitasking view, opening the Notification Center or in the event of an incoming call.

For more information, see React Native documentation.

Events

change

This event is received when the app state has changed. The listener is called with one of the current app state values.

memoryWarning, focus, blur, etc

Falls back to AppState

Methods

addEventListener()

static addEventListener(
  type: AppStateEvent,
  listener: (state: AppStateStatus) => void,
): NativeEventSubscription;

Sets up a function that will be called whenever the specified event type on Lifecycle occurs. Valid values for eventType are listed above. Returns the EventSubscription.

Properties

currentState

static currentState: AppStateStatus;

Jest mock

jest.mock('react-native-applifecycle/dist/AppLifecycle', () => require('react-native-applifecycle/jest/AppLifecycleMock'));

Contribute

New features, bug fixes and improvements are welcome! For questions and suggestions use the issues.

Become a Patron! Donate

Star History

Star History Chart

License

The MIT License (MIT)

Copyright (c) 2024 Douglas Nassif Roma Junior

See the full license file.

react-native-applifecycle's People

Contributors

douglasjunior avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-applifecycle's Issues

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference

First of all great work with the lib really appreciated as it very use full for my project.
Just wanted to bring your attention to one issue i recently identified below are the details

How did you link to the library (autolinking, manual, rn link, cocoapods)?
-> I used autolinking for the library.

What version of React Native?
-> I am using React Native version 0.64.1.

What version of the library?
-> The version of react-native-applifecycle I'm using is 0.4.0

iOS/Android version?
-> The error occurred on Android version 14.

Did the problem happen after updating React Native?
-> No, the problem occurred without any recent updates to React Native.

Are you using the library for the first time?
-> Yes, i am using this lib for the first time.

It's a bug? Provide a link to a minimal reproduction case.

-> Yes, it's a bug. and it occurred when i tried to trigger my app life cycle from app killed state(as foreground service for backup task)

Solution:

  • I have found the solution for this as well and i have tested that and it works
    Need to add null check for initialize() and invalidate()
    if(reactApplicationContext.getCurrentActivity() != null) { reactApplicationContext.getCurrentActivity().runOnUiThread(() -> ProcessLifecycleOwner .get() .getLifecycle() .addObserver(AppLifecycleModule.this) ); }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.