Skip to content

Commit

Permalink
fix android back on root tabs (keybase#21239)
Browse files Browse the repository at this point in the history
* fix android back on root tabs

* move task to background
  • Loading branch information
chrisnojima authored Nov 26, 2019
1 parent b431869 commit e278be6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
21 changes: 19 additions & 2 deletions shared/actions/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,15 @@ function* maybeDoneWithLogoutHandshake(state: Container.TypedState) {
}
}

let routeToInitialScreenOnce = false
let lastTab: Tabs.Tab | undefined
const stashLastRoute = (_state: Container.TypedState, action: ConfigGen.PersistRoutePayload) => {
const {path} = action.payload
if (path?.[1]?.routeName === 'Main') {
lastTab = path?.[2].routeName
}
}

let routeToInitialScreenOnce = false
const routeToInitialScreen2 = (state: Container.TypedState) => {
// bail if we don't have a navigator and loaded
if (!Router2._getNavigator()) {
Expand All @@ -372,7 +379,16 @@ const routeToInitialScreen2 = (state: Container.TypedState) => {
// We figure out where to go (push, link, saved state, etc) once ever in a session
const routeToInitialScreen = (state: Container.TypedState) => {
if (routeToInitialScreenOnce) {
return
if (state.config.loggedIn) {
// don't jump to a screen, just ensure you're logged in / out state is correct
return [
RouteTreeGen.createSwitchLoggedIn({loggedIn: true}),
RouteTreeGen.createSwitchTab({tab: (lastTab as any) || Tabs.peopleTab}),
]
} else {
// Show a login screen
return [RouteTreeGen.createSwitchLoggedIn({loggedIn: false})]
}
}
routeToInitialScreenOnce = true

Expand Down Expand Up @@ -690,6 +706,7 @@ function* configSaga() {
yield* Saga.chainAction2(ConfigGen.setNavigator, setNavigator)
// Go to the correct starting screen
yield* Saga.chainAction2([ConfigGen.daemonHandshakeDone, ConfigGen.setNavigator], routeToInitialScreen2)
yield* Saga.chainAction2(ConfigGen.persistRoute, stashLastRoute)

yield* Saga.chainAction2(ConfigGen.daemonHandshakeDone, emitStartupFirstIdle)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
public class MainActivity extends ReactFragmentActivity {
private static final String TAG = MainActivity.class.getName();
private PermissionListener listener;
static boolean createdReact = false;

@Override
public void invokeDefaultOnBackPressed() {
moveTaskToBack(true);
}

private static void createDummyFile(Context context) {
final File dummyFile = new File(context.getFilesDir(), "dummy.txt");
Expand Down Expand Up @@ -131,7 +137,11 @@ private String colorSchemeForCurrentConfiguration() {
@TargetApi(Build.VERSION_CODES.KITKAT)
protected void onCreate(Bundle savedInstanceState) {
ReactInstanceManager instanceManager = this.getReactInstanceManager();
instanceManager.createReactContextInBackground();
if (!this.createdReact) {
this.createdReact = true;
instanceManager.createReactContextInBackground();
}

setupKBRuntime(this, true);
super.onCreate(null);

Expand Down
38 changes: 2 additions & 36 deletions shared/router-v2/router.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {Props} from './router'
import {connect} from '../util/container'
import {createAppContainer} from '@react-navigation/native'
import {createBottomTabNavigator} from 'react-navigation-tabs'
import {createSwitchNavigator, StackActions} from '@react-navigation/core'
import {createSwitchNavigator} from '@react-navigation/core'
import debounce from 'lodash/debounce'
import {modalRoutes, routes, loggedOutRoutes, tabRoots} from './routes'
import {useScreens} from 'react-native-screens'
Expand Down Expand Up @@ -153,6 +153,7 @@ const VanillaTabNavigator = createBottomTabNavigator(
{blank: {screen: BlankScreen}}
),
{
backBehavior: 'none',
defaultNavigationOptions: ({navigation}) => {
const routeName = navigation.state.index && navigation.state.routes[navigation.state.index].routeName
const tabBarVisible = routeName !== 'chatConversation'
Expand Down Expand Up @@ -300,41 +301,6 @@ class RNApp extends React.PureComponent<Props> {
this.props.persistRoute(Constants.getVisiblePath())
}, 3000)

private handleAndroidBack = () => {
const nav = this.nav
if (!nav) {
return
}
const path = Constants.getVisiblePath()

// We determine if we're at the root if we're at the root of our navigation hierarchy, which is slightly different if you're logged in or out
if (path[0].routeName === 'loggedIn') {
if (path[1].routeName === 'Main') {
if (path.length === 3) {
return false
}
}
} else {
if (path.length === 2) {
return false
}
}
nav.dispatch(StackActions.pop({}))
return true
}

componentDidMount() {
if (Styles.isAndroid) {
Kb.NativeBackHandler.addEventListener('hardwareBackPress', this.handleAndroidBack)
}
}

componentWillUnmount() {
if (Styles.isAndroid) {
Kb.NativeBackHandler.removeEventListener('hardwareBackPress', this.handleAndroidBack)
}
}

getNavState = () => {
const n = this.nav
return (n && n.state && n.state.nav) || null
Expand Down

0 comments on commit e278be6

Please sign in to comment.