Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Torch stops working after going back from another screen 🐛 #1225

Closed
3 of 4 tasks
rkostrab opened this issue Sep 5, 2022 · 2 comments · Fixed by #1466
Closed
3 of 4 tasks

Android: Torch stops working after going back from another screen 🐛 #1225

rkostrab opened this issue Sep 5, 2022 · 2 comments · Fixed by #1466
Labels
🐛 bug Something isn't working

Comments

@rkostrab
Copy link

rkostrab commented Sep 5, 2022

What were you trying to do?

On Android...

  1. I want to show camera screen with torch='on'
  2. Then I want to navigate (using react-navigation) from camera screen to another screen (let's say TestScreen)
  3. When I go back to camera screen from Test screen, then torch isn't starting

This scenario is useful to someone who wants to take a picture, then go to preview screen and then wants to go back to camera screen. I've create full app code to replicate error scenario.

Reproduceable Code

import { View, ActivityIndicator, Button, Text } from 'react-native';
import React from 'react';
import { Camera, useCameraDevices } from 'react-native-vision-camera';
import { NavigationContainer, useIsFocused } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const CameraScreen = ({ navigation }) => {
  const isScreenFocused = useIsFocused()
  const devices = useCameraDevices()
  const device = devices.back

  return !device ? <ActivityIndicator style={{ flex: 1 }} /> : (
    <View style={{ flex: 1 }}>
      <Camera
        style={{ flex: 1 }}
        device={device}
        torch='on'
        isActive={isScreenFocused}/>
      <Button title='Go to TestScreen' onPress={() => navigation.navigate('TestScreen')} />
    </View>
  )
}

const TestScreen = () => <Text style={{ flex: 1, color: 'black' }}>TestScreen</Text>

const Stack = createStackNavigator();
const Navigator = () => (
  <Stack.Navigator>
    <Stack.Screen name='CameraScreen' component={CameraScreen} />
    <Stack.Screen name='TestScreen' component={TestScreen} />
  </Stack.Navigator>
)

const App = () => (
  <NavigationContainer>
    <Navigator />
  </NavigationContainer>
)

export default App;

What happened instead?

Torch not starting after going back to camera

Relevant log output

No response

Device

Android

VisionCamera Version

2.14.1

Additional information

@rkostrab rkostrab added the 🐛 bug Something isn't working label Sep 5, 2022
@rkostrab
Copy link
Author

rkostrab commented Sep 5, 2022

Here's a (weird, but working) workaround for anyone facing this issue:

...

-import React from 'react';
+import React, { useCallback, useState } from 'react';
-import { NavigationContainer, useIsFocused } from '@react-navigation/native';
+import { NavigationContainer, useIsFocused, useFocusEffect } from '@react-navigation/native';

const CameraScreen = ({ navigation }) => {
  const isScreenFocused = useIsFocused()
  const devices = useCameraDevices()
  const device = devices.back
  
+  const [isTorch, setIsTorch] = useState(false)
+  useFocusEffect(useCallback(() => {
+    setIsTorch(true);
+    return () => setIsTorch(false);
+  }, []))

  return !device ? <ActivityIndicator style={{ flex: 1 }} /> : (
    <View style={{ flex: 1 }}>
      <Camera
        style={{ flex: 1 }}
        device={device}
-        torch='on'
+        torch={isTorch ? 'on' : 'off'}
        isActive={isScreenFocused}/>
      <Button title='Go to TestScreen' onPress={() => navigation.navigate('TestScreen')} />
    </View>
  )
}

...

@mrousavy
Copy link
Owner

Hey! I've rewritten the entire Android codebase of VisionCamera from CameraX to Camera2 in the efforts of ✨ VisionCamera V3.

I just now completed the Camera2 rewrite and I believe the core structure is running, but there might be some edge cases to iron out. Can you try and test the PR #1674 for me to see if you can still reproduce this issue here?

Here's an instruction on how you can test that: #1674 (comment)

If the issue cannot be reproduced with that version/PR anymore, then hoorayy, I fixed it! 🎉
Otherwise please let me know and I'll keep this issue open to keep track of it.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants