ignite-bowser icon indicating copy to clipboard operation
ignite-bowser copied to clipboard

Android can't click push to new screen after used BackHandler back

Open pheaktratyboy opened this issue 5 years ago • 13 comments

Hi, I have a problem with Android BackHandler after I init a new project with ignite bowser and I was run the project and click Android Back Button Press the app it exit but the problem when I have re-open the app and click button continue push to another screen it not work. Can you help me, please? and provide solution for this issue.

Screenshot_1575304205

Screenshot_1575304298

Screenshot_1575304318

pheaktratyboy avatar Dec 02 '19 16:12 pheaktratyboy

+1

lyseiha avatar Dec 03 '19 02:12 lyseiha

same problem

liwenkangatom avatar Jan 03 '20 03:01 liwenkangatom

use mobx-react instead of mobx-react-lite。

liwenkangatom avatar Jan 06 '20 01:01 liwenkangatom

@liwenkangatom do you have any idea why mobx-react-lite doesn't work??

chakrihacker avatar Jan 08 '20 19:01 chakrihacker

Using react-mobx still have same issue

frdteknikelektro avatar Feb 02 '20 06:02 frdteknikelektro

hey boys , how to deal with it now

Sent with GitHawk

summxu avatar Feb 20 '20 09:02 summxu

useBackButtonHandler() is not working. (Ignite-bowser-5.1.0)

I have set 2 screens in canExit. canExit = ["welcome", "home"] but when press back from home screen then navigation called goBack().

can anyone help me to solve this issue

pragneshpj avatar Jun 05 '20 13:06 pragneshpj

@pragneshpj maybe this helps https://github.com/infinitered/ignite-bowser/issues/347#issuecomment-636271156

dakkafex avatar Jun 06 '20 04:06 dakkafex

@dakkafex useBackButtonHandler() is not working. i want to exit app from 2 screen. -> welcome screen (from auth stack)& -> home screen (drawer stack)

there are so many screen in auth-stack navigator and drawer-stack navigator.

canExit = ["welcome", "home"] is working perfectly in 4.13.0.

pragneshpj avatar Jun 16 '20 07:06 pragneshpj

have any one found the solutions? I have same issue in ignite bowser new version.

variyanirav avatar Jun 16 '20 09:06 variyanirav

Update your use-back-button-handler.tsx with this, got it from here

import { useEffect, useRef, useState } from "react"
import { BackHandler, DeviceEventEmitter } from "react-native"
import { NavigationContainerRef } from "@react-navigation/native"
import getActiveRouteName from "./get-active-routename"

export function useBackButtonHandler(
  ref: React.RefObject<NavigationContainerRef>,
  canExit: (routeName: string) => boolean,
) {
  const [backPressSubscriptions] = useState(new Set())
  const canExitRef = useRef(canExit)

  useEffect(() => {
    canExitRef.current = canExit
  }, [canExit])

  useEffect(() => {
    // We'll fire this when the back button is pressed on Android.
    const handleBackPress = () => {
      const navigation = ref.current

      if (navigation == null) {
        return false
      }

      // grab the current route
      const routeName = getActiveRouteName(navigation.getRootState())

      // are we allowed to exit?
      if (canExitRef.current(routeName)) {
        // let the system know we've not handled this event
        return false
      }

      // we can't exit, so let's turn this into a back action
      if (navigation.canGoBack()) {
        navigation.goBack()
        return true
      }

      return false
    }

    // Subscribe when we come to life
    DeviceEventEmitter.removeAllListeners("hardwareBackPress")
    DeviceEventEmitter.addListener("hardwareBackPress", () => {
      let invokeDefault = true
      const subscriptions = []

      backPressSubscriptions.forEach(sub => subscriptions.push(sub))

      for (let i = 0; i < subscriptions.reverse().length; i += 1) {
        if (subscriptions[i]()) {
          invokeDefault = false
          break
        }
      }

      if (invokeDefault) {
        BackHandler.exitApp()
      }
    })

    backPressSubscriptions.add(handleBackPress)

    // Unsubscribe when we're done
    return () => {
      DeviceEventEmitter.removeAllListeners("hardwareBackPress")
      backPressSubscriptions.clear()
    }
  }, [ref])
}

archansel avatar Jun 19 '20 04:06 archansel

Warning using my solution, it will remove all hardwareBackPress event listener from other libs too. correct way to handle back button in react navigation 5 is https://reactnavigation.org/docs/custom-android-back-button-handling but I'm still struggling to make it work flawlessly

archansel avatar Jun 25 '20 06:06 archansel

I created separate issue #357 because it's not relevant with this issue

archansel avatar Jun 25 '20 09:06 archansel