Ryan Fonseka
Ryan Fonseka

Reputation: 255

Android back button is applied to all the screens react native Error

I have added a function to disable the back button press in particular screen on my app. after the I have gone away from this screen back button is disabled in the whole app. The back button is disabled after that. following is my code

useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
        (async () => {

            // console.log('param name.....',itemId)
            const time =  await AsyncStorage.getItem('@timer_set')
            setEndTime(time);
            const backhandler = BackHandler.addEventListener('hardwareBackPress', () => {
                return true;
              });
              return () => {
                  backhandler.remove();
              };
          })();
    });
    return unsubscribe;
  }, [navigation]);

Upvotes: 0

Views: 417

Answers (1)

Michael Bahl
Michael Bahl

Reputation: 3669

Use BackHandler.removeEventListener.

 useEffect(() => {
   const backButtonHandler = () => {
      return true;
    }
    BackHandler.addEventListener("hardwareBackPress", backButtonHandler);

    return () =>
      BackHandler.removeEventListener("hardwareBackPress", backButtonHandler);
  }, []);

Upvotes: 1

Related Questions