question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`onDetachedFromWindow` triggered incorrectly

See original GitHub issue

Description

View that was part ScreenContainer received onDetachedFromWindow multiple times or before onAttachedToWindow (unlikely) causing this Google Cast Button View to try and unregister an un-registered receiver. This doesn’t happen when I set enabledScreens(false)

Fatal Exception: java.lang.IllegalArgumentException: Receiver not registered: androidx.mediarouter.app.MediaRouteButton$ConnectivityReceiver@82cf304
       at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1499)
       at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1605)
       at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:678)
       at androidx.mediarouter.app.MediaRouteButton$ConnectivityReceiver.unregisterReceiver(MediaRouteButton.java:871)
       at androidx.mediarouter.app.MediaRouteButton.onDetachedFromWindow(MediaRouteButton.java:562)
       at com.reactnative.googlecast.components.RNGoogleCastButtonManager$ColorableMediaRouteButton.onDetachedFromWindow(RNGoogleCastButtonManager.java:111)
       at android.view.View.dispatchDetachedFromWindow(View.java:21358)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:6499)
       at android.view.ViewGroup.removeAllViews(ViewGroup.java:6445)
       at com.swmansion.rnscreens.ScreenContainer.onDetachedFromWindow(ScreenContainer.java:309)
       at android.view.View.dispatchDetachedFromWindow(View.java:21358)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4649)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:6287)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:6258)
       at android.view.ViewGroup.removeView(ViewGroup.java:6189)
       at com.swmansion.rnscreens.ScreenContainer.removeView(ScreenContainer.java:84)
       at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1250)
       at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
       at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
       at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1504)
       at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2007)
       at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
       at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
       at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:8167)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

Package versions

  • React: 16.13.1
  • React Native: 0.63.4
  • React Native Screens: 3.1.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
YangJonghuncommented, Jun 17, 2021

@WoLewicki #985 fixed this issue

2reactions
YangJonghuncommented, May 26, 2021

@kacperkapusciak

$ npx react-native init MyApp --template react-native-template-typescript@6.5.13 $ yarn add react-native-google-cast $ yarn add <react-navigation’s required dependencies>

and apply required configs https://react-native-google-cast.github.io/docs/getting-started/installation#android https://react-native-google-cast.github.io/docs/getting-started/setup#android https://github.com/software-mansion/react-native-screens#android https://docs.swmansion.com/react-native-gesture-handler/docs/#android

  • MainActivity.java
import com.google.android.gms.cast.framework.CastContext;
 ...
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    // lazy load Google Cast context
    CastContext.getSharedInstance(this);
  }

  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
      @Override
      protected ReactRootView createRootView() {
        return new RNGestureHandlerEnabledRootView(MainActivity.this);
      }
    };
  }
  ...
  • App.tsx
import React from 'react';
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { View, Text } from 'react-native';
import { CastButton } from 'react-native-google-cast';

const BottomTab = createBottomTabNavigator()
const Tab1Stack = createStackNavigator()

const CastScreen = () => (
  <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
    <Text style={{marginBottom:20, textAlign:"center"}}>Please make sure "CastButton" rendered below{'\n'}and don't connect any receiver</Text>
    <CastButton style={{tintColor:"black"}}/>
    <Text style={{position:"absolute", bottom:0, alignSelf:"flex-end"}}>↓touch Tab2↓</Text>
  </View>
)

const Tab2Screen = () => (
  <View />
)

const Tab1StackNavigator = () => (
  <Tab1Stack.Navigator initialRouteName="CastScreen">
    <Tab1Stack.Screen name="CastScreen" component={CastScreen} />
  </Tab1Stack.Navigator>
)

const App = () => (
  <NavigationContainer>
    <BottomTab.Navigator initialRouteName="Tab1">
      <BottomTab.Screen name="Tab1" component={Tab1StackNavigator} />
      <BottomTab.Screen name="Tab2" component={Tab2Screen} />
    </BottomTab.Navigator>
  </NavigationContainer>
);

 export default App;

also I created a sample private repo and I invited you as a collaborator

Read more comments on GitHub >

github_iconTop Results From Across the Web

CustomView lifecycle onDetachedFromWindow() not fired
I just removed onAttachedToWindow/onDetachedFromWindow and added onWindowVisibilityChanged instead. What I would like to know is drawbacks of ...
Read more >
Developers - `onDetachedFromWindow` triggered incorrectly -
View that was part ScreenContainer received onDetachedFromWindow twice or before onAttachedToWindow (unlikely) causing this Google Cast Button View to try ...
Read more >
Android leak pattern: subscriptions in views - Square Developer
I was recently looking at a LeakCanary report, which was caused by ... onDetachedFromWindow() was not being called, which created the leak.
Read more >
DataSetObserver gets unregistered twice [36936923]
onDetachedFromWindow () gets called twice when the activity restarts meaning that, incorrectly, Android tries to unregister a null DataSetObserver.
Read more >
806108 - Zillow App Crash: LollipopWebContentsAccessibility
When the class calls onAttachToWindow() it will try/catch the register of their BroadcastReceiver but in the onDetachedFromWindow() they do ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found