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: showNativeVolumeUI stops working after app goes in the background and comes back #29

Open
matiastucci opened this issue Jan 31, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@matiastucci
Copy link

I'm hiding the native UI on a section of my app but I found that if I lock the screen and then turn it on again, the native UI starts appearing. I tried hiding it when the app state is active but no luck. FYI, this is only happening on Android. Any ideas?

 useEffect(() => {
   // Hide it as soon as entering the section
    VolumeManager.showNativeVolumeUI({ enabled: false });
  }, []);

  useEffect(() => {
    const listener = AppState.addEventListener('change', (nextAppState) => {
      // Hide it as soon as entering the section
      VolumeManager.showNativeVolumeUI({ enabled: nextAppState !== 'active' });
    });

    return () => {
      listener.remove();
     // Show it when leaving the section
      VolumeManager.showNativeVolumeUI({ enabled: true });
    };
  }, []);
@hirbod hirbod added the needs review Need to be reviewed label Feb 1, 2024
@matiastucci
Copy link
Author

Another thing I noticed is that after setting showNativeVolumeUI to true, setting it to false doesn't work anymore.

E.g:

useEffect(() => {
  VolumeManager.showNativeVolumeUI({ enabled: true });

  setTimeout(() => {
    // doesn't work :(
    VolumeManager.showNativeVolumeUI({ enabled: false });
  }, 10000);
}, []);

but setting it to false and then true works fine.

Any ideas? Thanks!

@Kieraano
Copy link

Kieraano commented Feb 14, 2024

I'm getting the same issue on iOS.

Once I call:
VolumeManager.showNativeVolumeUI({ enabled: true });

Calling this doesn't work as you would expect:
VolumeManager.showNativeVolumeUI({ enabled: false });

Basically once you hide the native volume UI you can't bring it back.

@hirbod
Copy link
Owner

hirbod commented Feb 14, 2024

There might be a logic issue here

I have to check this once I have a bit more time.

if (!flag && [strongSelf->customVolumeView superview]) {
  [strongSelf->customVolumeView removeFromSuperview];
} else if (flag && ![strongSelf->customVolumeView superview]) {
  // Potentially adjust the frame as needed, not necessarily CGRectZero
  strongSelf->customVolumeView.frame = CGRectMake(0, 0, 0, 0);
  UIViewController *topViewController = [strongSelf topMostViewController];
  [topViewController.view addSubview:strongSelf->customVolumeView];
}

might do the trick but I am not 100% sure yet.

@Kieraano
Copy link

The below code change fixed the issue for me on iOS.

On reflection, this is likely a different issue to what matiastucci experienced.

    if (flag) {
      // To show the native volume UI, ensure our customVolumeView is removed from its superview
      if ([strongSelf->customVolumeView superview]) {
        [strongSelf->customVolumeView removeFromSuperview];
      }
    } else {
      // To hide the native volume UI, add our customVolumeView offscreen if it's not already added
      if (![strongSelf->customVolumeView superview]) {
        // Ensure the customVolumeView is offscreen and not interfering visually
        strongSelf->customVolumeView.frame = CGRectMake(0, 0, 0, 0); // Set the frame to CGRectZero

        // Find the topMostViewController to add the customVolumeView to its view hierarchy
        UIViewController *topViewController = [strongSelf topMostViewController];
        if (topViewController) {
          [topViewController.view addSubview:strongSelf->customVolumeView];
        }
      }
    }`

@hirbod
Copy link
Owner

hirbod commented Feb 15, 2024

@Kieraano yes, @matiastucci issue is related to Android. I have some ideas. I will also verify your suggestion and create some more test cases and release a new version pretty soon

@hirbod hirbod added bug Something isn't working and removed needs review Need to be reviewed labels Feb 15, 2024
@rajdivergenceneuro
Copy link

rajdivergenceneuro commented Feb 26, 2024

Related issue(iOS)

  • showNativeVolumeUI is not working, when app is in background state.

Step to produce

  • Add the volume manage in background service (React-Native-Ble-Manager, React-Native-Background-Timer).
  • Increase random volume, it is showing UI even it is set to false.

@hirbod
Copy link
Owner

hirbod commented Feb 26, 2024

I don't think its possible to hide the volume toast when the app is backgrounded.

@rajdivergenceneuro
Copy link

I don't think its possible to hide the volume toast when the app is backgrounded.

Ok, Thanks for quick reply!

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

No branches or pull requests

4 participants