Bob Kris
Bob Kris

Reputation: 83

React native web view Android back button not exit the app

React native web view Android back button not exit the app. When i pressed back button it will not exixt the app. The following are my sample code.

import React, { useEffect, useRef } from 'react';
import { BackHandler } from 'react-native';
import WebView from 'react-native-webview';

const App = () => {
    const webview = useRef(null);
    const onAndroidBackPress = () => {
    if (webview.current) {
      webview.current.goBack();
      return true;
    }
    return false;
    };

    useEffect(() => {
    BackHandler.addEventListener('hardwareBackPress', onAndroidBackPress);

    return () => {
      BackHandler.removeEventListener('hardwareBackPress', onAndroidBackPress);
    };
    }, []);

    return (
    <WebView source={{ uri: 'https://google.com' }} ref={webview}/>
    )
}
export default App;

Upvotes: 0

Views: 896

Answers (1)

Tarik
Tarik

Reputation: 597

There is a method in Backhandler for exiting app. Did you try it?

Upvotes: 1

Related Questions