React-native Android - Get The Variables From Intent
I'm using intent to start my React-Native app, and I'm trying to find out how to get the variables I put on my intent in the react native code. Is this possible from within react-n
Solution 1:
Try this to get Intent params at react-native app.
In my native App, I use this code:
IntentlaunchIntent= getPackageManager().getLaunchIntentForPackage("com.my.react.app.package");
launchIntent.putExtra("test", "12331");
startActivity(launchIntent);
In react-native project, my MainActivity.java
publicclassMainActivityextendsReactActivity {
@Overrideprotected String getMainComponentName() {
return"FV";
}
publicstaticclassTestActivityDelegateextendsReactActivityDelegate {
privatestaticfinalStringTEST="test";
privateBundlemInitialProps=null;
privatefinal@Nullable
Activity mActivity;
publicTestActivityDelegate(Activity activity, String mainComponentName) {
super(activity, mainComponentName);
this.mActivity = activity;
}
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
Bundlebundle= mActivity.getIntent().getExtras();
if (bundle != null && bundle.containsKey(TEST)) {
mInitialProps = newBundle();
mInitialProps.putString(TEST, bundle.getString(TEST));
}
super.onCreate(savedInstanceState);
}
@Overrideprotected Bundle getLaunchOptions() {
return mInitialProps;
}
}
@Overrideprotected ReactActivityDelegate createReactActivityDelegate() {
returnnewTestActivityDelegate(this, getMainComponentName());
}
}
In my first Container I get the param in this.props
exportdefaultclassAppextendsComponent {
render() {
console.log('App props', this.props);
//...
}
}
The complete example I found here: http://cmichel.io/how-to-set-initial-props-in-react-native/
Solution 2:
You can pass initial props as a bundle to the third parameter in the startReactApplication
method, like so:
BundleinitialProps=newBundle();
initialProps.putString("alarm", true);
mReactRootView.startReactApplication( mReactInstanceManager, "HelloWorld", initialProps );
See this answer for more details: https://stackoverflow.com/a/34226172/293280
Solution 3:
I think this more correct way based on answer of @Eduardo Junior
classMainDelegate(activity: ReactActivity, mainComponentName: String?) :
ReactActivityDelegate(activity, mainComponentName) {
privatevar params: Bundle? = nulloverridefunonCreate(savedInstanceState: Bundle?) {
params = plainActivity.intent.extras
super.onCreate(savedInstanceState)
}
overridefunonNewIntent(intent: Intent?): Boolean {
params = intent?.extras
returnsuper.onNewIntent(intent)
}
overridefungetLaunchOptions() = params
}
classMainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/overridefungetMainComponentName() = "example"overridefuncreateReactActivityDelegate(): ReactActivityDelegate {
return MainDelegate(this, mainComponentName)
}
}
Post a Comment for "React-native Android - Get The Variables From Intent"