From a51a98513d09f2742bd76a31506bcec93e0e19d6 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Fri, 15 Jan 2021 14:49:00 -0300 Subject: [PATCH] [FIX] Back button closing activity when on root stack screen (#2804) * Make hardware back button to behave as home button on root screens * Remove unnecessary code * Remove handleBackPress from OnboardingView * Fix lint --- android/app/src/main/AndroidManifest.xml | 2 +- .../chat/rocket/reactnative/MainActivity.java | 5 +++ app/AppContainer.js | 4 +- app/actions/app.js | 1 - app/views/OnboardingView/index.js | 38 +------------------ app/views/RoomsListView/index.js | 4 -- 6 files changed, 10 insertions(+), 44 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8c3ab8d18..440766f18 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -29,7 +29,7 @@ android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:exported="true" android:label="@string/app_name" - android:launchMode="singleTask" + android:launchMode="singleTop" android:windowSoftInputMode="adjustResize"> diff --git a/android/app/src/main/java/chat/rocket/reactnative/MainActivity.java b/android/app/src/main/java/chat/rocket/reactnative/MainActivity.java index e65fc28b3..e93eb3f96 100644 --- a/android/app/src/main/java/chat/rocket/reactnative/MainActivity.java +++ b/android/app/src/main/java/chat/rocket/reactnative/MainActivity.java @@ -37,6 +37,11 @@ public class MainActivity extends ReactFragmentActivity { RNBootSplash.init(R.drawable.launch_screen, MainActivity.this); } + @Override + public void invokeDefaultOnBackPressed() { + moveTaskToBack(true); + } + /** * Returns the name of the main component registered from JavaScript. This is used to schedule * rendering of the component. diff --git a/app/AppContainer.js b/app/AppContainer.js index 4023635f7..9ec17ad43 100644 --- a/app/AppContainer.js +++ b/app/AppContainer.js @@ -7,7 +7,7 @@ import { connect } from 'react-redux'; import Navigation from './lib/Navigation'; import { defaultHeader, getActiveRouteName, navigationTheme } from './utils/navigation'; import { - ROOT_LOADING, ROOT_OUTSIDE, ROOT_NEW_SERVER, ROOT_INSIDE, ROOT_SET_USERNAME, ROOT_BACKGROUND + ROOT_LOADING, ROOT_OUTSIDE, ROOT_NEW_SERVER, ROOT_INSIDE, ROOT_SET_USERNAME } from './actions/app'; // Stacks @@ -65,7 +65,7 @@ const App = React.memo(({ root, isMasterDetail }) => { > <> - {root === ROOT_LOADING || root === ROOT_BACKGROUND ? ( + {root === ROOT_LOADING ? ( { - this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress); - }); - this.unsubscribeBlur = navigation.addListener('blur', () => { - if (this.backHandler && this.backHandler.remove) { - this.backHandler.remove(); - } - }); - } - shouldComponentUpdate(nextProps) { const { theme } = this.props; if (theme !== nextProps.theme) { @@ -54,21 +39,6 @@ class OnboardingView extends React.Component { return false; } - componentWillUnmount() { - if (this.unsubscribeFocus) { - this.unsubscribeFocus(); - } - if (this.unsubscribeBlur) { - this.unsubscribeBlur(); - } - } - - handleBackPress = () => { - const { appStart } = this.props; - appStart({ root: ROOT_BACKGROUND }); - return false; - } - connectServer = () => { logEvent(events.ONBOARD_JOIN_A_WORKSPACE); const { navigation } = this.props; @@ -116,8 +86,4 @@ class OnboardingView extends React.Component { } } -const mapDispatchToProps = dispatch => ({ - appStart: params => dispatch(appStartAction(params)) -}); - -export default connect(null, mapDispatchToProps)(withTheme(OnboardingView)); +export default withTheme(OnboardingView); diff --git a/app/views/RoomsListView/index.js b/app/views/RoomsListView/index.js index 8d39995df..6ec23f29c 100644 --- a/app/views/RoomsListView/index.js +++ b/app/views/RoomsListView/index.js @@ -29,7 +29,6 @@ import { roomsRequest as roomsRequestAction, closeServerDropdown as closeServerDropdownAction } from '../../actions/rooms'; -import { appStart as appStartAction, ROOT_BACKGROUND } from '../../actions/app'; import debounce from '../../utils/debounce'; import { isIOS, isTablet } from '../../utils/deviceInfo'; import RoomsListHeaderView from './Header'; @@ -549,12 +548,10 @@ class RoomsListView extends React.Component { handleBackPress = () => { const { searching } = this.state; - const { appStart } = this.props; if (searching) { this.cancelSearch(); return true; } - appStart({ root: ROOT_BACKGROUND }); return false; }; @@ -1044,7 +1041,6 @@ const mapDispatchToProps = dispatch => ({ toggleSortDropdown: () => dispatch(toggleSortDropdownAction()), openSearchHeader: () => dispatch(openSearchHeaderAction()), closeSearchHeader: () => dispatch(closeSearchHeaderAction()), - appStart: params => dispatch(appStartAction(params)), roomsRequest: params => dispatch(roomsRequestAction(params)), selectServerRequest: server => dispatch(selectServerRequestAction(server)), closeServerDropdown: () => dispatch(closeServerDropdownAction())