forked from vergnet/application-amicale
Handle android back button to go back in the webview
This commit is contained in:
parent
50334fb609
commit
08de6765a7
1 changed files with 31 additions and 0 deletions
|
@ -9,6 +9,7 @@ import MaterialHeaderButtons, {Item} from '../Custom/HeaderButton';
|
||||||
import {HiddenItem} from "react-navigation-header-buttons";
|
import {HiddenItem} from "react-navigation-header-buttons";
|
||||||
import {Linking} from "expo";
|
import {Linking} from "expo";
|
||||||
import i18n from 'i18n-js';
|
import i18n from 'i18n-js';
|
||||||
|
import {BackHandler} from "react-native";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
navigation: Object,
|
navigation: Object,
|
||||||
|
@ -27,9 +28,12 @@ class WebViewScreen extends React.PureComponent<Props> {
|
||||||
|
|
||||||
webviewRef: Object;
|
webviewRef: Object;
|
||||||
|
|
||||||
|
canGoBack: boolean;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.webviewRef = React.createRef();
|
this.webviewRef = React.createRef();
|
||||||
|
this.canGoBack = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,8 +44,32 @@ class WebViewScreen extends React.PureComponent<Props> {
|
||||||
this.props.navigation.setOptions({
|
this.props.navigation.setOptions({
|
||||||
headerRight: rightButton,
|
headerRight: rightButton,
|
||||||
});
|
});
|
||||||
|
this.props.navigation.addListener(
|
||||||
|
'focus',
|
||||||
|
() =>
|
||||||
|
BackHandler.addEventListener(
|
||||||
|
'hardwareBackPress',
|
||||||
|
this.onBackButtonPressAndroid
|
||||||
|
)
|
||||||
|
);
|
||||||
|
this.props.navigation.addListener(
|
||||||
|
'blur',
|
||||||
|
() =>
|
||||||
|
BackHandler.removeEventListener(
|
||||||
|
'hardwareBackPress',
|
||||||
|
this.onBackButtonPressAndroid
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onBackButtonPressAndroid = () => {
|
||||||
|
if (this.canGoBack){
|
||||||
|
this.onGoBackClicked();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a header refresh button
|
* Gets a header refresh button
|
||||||
*
|
*
|
||||||
|
@ -99,6 +127,9 @@ class WebViewScreen extends React.PureComponent<Props> {
|
||||||
errorCode={ERROR_TYPE.CONNECTION_ERROR}
|
errorCode={ERROR_TYPE.CONNECTION_ERROR}
|
||||||
onRefresh={this.onRefreshClicked}
|
onRefresh={this.onRefreshClicked}
|
||||||
/>}
|
/>}
|
||||||
|
onNavigationStateChange={navState => {
|
||||||
|
this.canGoBack = navState.canGoBack;
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue