Tried to show an alert while not attached to an Activity

See original GitHub issue
  • android 7.0
  • react-native 0.43
  • when i use the ‘alert’ in an android app which integrated the react-native the code after:

	import React, {Component} from 'react';
	import {
	    AppRegistry,
	    StyleSheet,
	    Text,
	    View,
	    Navigator,
	    Button,
	    Alert,
	} from 'react-native';
	
	
	class HelloWorld extends React.Component {
	    show(){
	        Alert.alert(
	            'Alert Title',
	            'alertMessage',
	        )
	    }
	    render() {
	        return (
	            < View
	                style={styles.container
	                }>
	                <Text
	                    style={styles.hello
	                    }>
	                    Hello, World </ Text >
	                <Button title="a"
	                        onPress={this.show.bind(this)
	                        }/>
	            </ View >
	        )
	    }
	}
	
	var styles = StyleSheet.create({
	    container: {
	        flex: 1,
	        justifyContent: 'center',
	    },
	    hello: {
	        fontSize: 20,
	        textAlign: 'center',
	        margin: 10,
	    },
	});
	
	AppRegistry.registerComponent('a', () => HelloWorld);

it give me the warning like this and no dialog show

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
saghulcommented, May 23, 2017

I had the same problem, and I solved it by hooking my custom activity’s (the one which renders the ReactNativeRootView) lifecycle methods to the ReactNativeInstanceManager as the docs describe: http://facebook.github.io/react-native/releases/0.42/docs/integration-with-existing-apps.html#add-native-code

Specifically I added onPause, onResume, onDestroy and onBackPressed (though you don’t need the last one). By reading the code here you’ll see that the current activity is saved with those.

I hope that helps. Cheers!

0reactions
MarkOSullivan94commented, Jun 26, 2018

For anyone who is experiencing this issue while using RN in a fragment, make sure you have defined the current activity for the ReactInstanceManager.

Example:

 mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getActivity().getApplication())
                .setCurrentActivity(getActivity())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModulePath("index")
                .addPackage(new MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
Read more comments on GitHub >

github_iconTop Results From Across the Web

WARN Tried to show an alert while not attached to an Activity ...
Sometimes i get this error, but i really dont know whats causing this or where its come from, this error only appears when...
Read more >
tried to show an alert while not attached to an activity
今天使用alert来弹出消息时候,发现下方一直弹出一个黄色的错误提示:tried to show an alert while not attached to an activity,使用的是Android ...
Read more >
Using DialogFragment | CodePath Android Cliffnotes
This is typically used for displaying an alert dialog, a confirm dialog, or prompting the user for information within an overlay without having...
Read more >
DialogFragment - Android Developers
Control of the dialog (deciding when to show, hide, dismiss it) should be done through ... Called when the fragment is no longer...
Read more >
Android Fragment not attached to a context | by Weidian Huang
This is a common issue if we don't take care of it in our application, and it is difficult to catch the issue...
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