3

My react application is request for pin when app state change from background to active. The problem is my application have camera activity and after user take a photo, the app state is change from background to active.

I don't want my app to request pin after user take a photo so i need to detect that going to camera activity is not background state. What i have to do?

Note: 1. it's work well on ios 2. i try react native AppState has no 'inactive' state on Android but it's not work (it might because my application usr react-native-navigation and MainActivity is extend SplashActivity)

1 Answers1

2

My solution for this was

(defn next-state [previous current]
  (case previous
    "in-other-app" (case current
                     "active" "back-from-other-app"
                     "inactive" "in-other-app"
                     "background" "in-other-app")
    "back-from-other-app" current
    current))

(def previous-state-atom (atom (.-currentState app-state)))
(defn add-app-state-listener [f]
  (.addEventListener app-state "change"
    (fn [state]
      (let [current-state (next-state @previous-state-atom state)]
        (f current-state)
        (reset! previous-state-atom current-state)))))

Basically i keep track of what the previous state was and used that and the next state to fire a custom event (i.e. trigger a function with a string a the event)

Hope this helps somebody...

Edit:

i forgot about this:

(defn mark-open-other-app!
  "!!!MUST CALL!!! before any call that results in the opening of another app
  like ImagePicker/showImagePicker or c/open-link."
  []
  (when (= :android (di/device))
    (reset! previous-state-atom "in-other-app")))

to set the correct state when going into another app.

(defn cancel-open-other-app!
  "Api's like ImagePicker/showImagePicker can be cancelled in which case this function must be called to reset the app state"
  []
  (when (= :android (di/device))
    (reset! previous-state-atom "active")))

and this one to setup stuff back when you cancel.

boogie666
  • 650
  • 10
  • 24