Skip to content

fix(android,v10): support navigating away and back from a sscreen wit…#2217

Merged
mfazekas merged 1 commit intomainfrom
mfazekas/android-nav-fix
Sep 15, 2022
Merged

fix(android,v10): support navigating away and back from a sscreen wit…#2217
mfazekas merged 1 commit intomainfrom
mfazekas/android-nav-fix

Conversation

@mfazekas
Copy link
Contributor

@mfazekas mfazekas commented Sep 15, 2022

…h map

Fixes: #2212

Not sure if this is good way to go, but seems to solve the issue. We attache a lifecycle to the MapView and we handle events on onDetachedFromWindow and onAttachedToWindow

@mfazekas mfazekas force-pushed the mfazekas/android-nav-fix branch from 6ae12ba to 935f41c Compare September 15, 2022 18:52
@mfazekas
Copy link
Contributor Author

Used this example to reproduce the issue:

import React, { useState } from 'react';
import {
  Button,
  Image,
  Pressable,
  View,
  StyleSheet,
  Modal,
  Text,
} from 'react-native';
import MapboxGL, {
  MapView,
  ShapeSource,
  SymbolLayer,
  CircleLayer,
  Camera,
} from '@rnmapbox/maps';

const styles = {
  mapView: { flex: 1 },
  circleLayer: {
    circleRadiusTransition: { duration: 5000, delay: 0 },
    circleColor: '#ff0000',
  },
};

const features = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      id: 'a-feature',
      properties: {
        icon: 'example',
        text: 'example-icon-and-label',
      },
      geometry: {
        type: 'Point',
        coordinates: [-74.00597, 40.71427],
      },
    },
    {
      type: 'Feature',
      id: 'b-feature',
      properties: {
        text: 'just-label',
      },
      geometry: {
        type: 'Point',
        coordinates: [-74.001097, 40.71527],
      },
    },
    {
      type: 'Feature',
      id: 'c-feature',
      properties: {
        icon: 'example',
      },
      geometry: {
        type: 'Point',
        coordinates: [-74.00697, 40.72427],
      },
    },
  ],
};

class BugReportExample extends React.Component {
  state = {
    radius: 20,
    modalVisbile: false,
    showMap: false,
  };

  render() {
    const { navigation } = this.props;
    const { modalVisbile, showMap } = this.state;
    const circleLayerStyle = {
      ...styles.circleLayer,
      ...{ circleRadius: this.state.radius },
    };

    console.log('+++ modalVisible', modalVisbile);
    console.log('### rendering...');

    return (
      <>
        <Button
          title="Grow"
          onPress={() => this.setState({ radius: this.state.radius + 20 })}
        />
        <Button
          title="Modal"
          onPress={() => this.setState({ modalVisbile: true })}
        />
        <Button
          title="Toggle map"
          onPress={() => this.setState({ showMap: !showMap })}
        />
        <Button
          title="Navigate"
          onPress={() => navigation.navigate('ScreenWithoutMap')}
        />
        <Modal
          animationType="slide"
          transparent={false}
          visible={modalVisbile}
          onRequestClose={() => {
            this.setState({ modalVisbile: false });
          }}
        >
          <Text>this is a modal</Text>
          <Button
            title="close"
            onPress={() => {
              this.setState({ modalVisbile: false });
            }}
          />
          <MapView style={{ flex: 1 }} />
        </Modal>
        {showMap && (
          <MapView style={styles.mapView}>
            <Camera centerCoordinate={[-74.00597, 40.71427]} zoomLevel={14} />
            <ShapeSource id={'shape-source-id-0'} shape={features}>
              <CircleLayer id={'circle-layer'} style={circleLayerStyle} />
              <SymbolLayer
                id="symbol-id"
                style={{
                  iconImage: ['get', 'icon'],
                }}
              />
            </ShapeSource>
          </MapView>
        )}
      </>
    );
  }
}

export default BugReportExample;

@mfazekas mfazekas merged commit e606d6d into main Sep 15, 2022
@mfazekas mfazekas deleted the mfazekas/android-nav-fix branch September 15, 2022 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: (V10) Android freeze when using with react navigation

1 participant