React-Native Android - get variables from intentions

I use the intention to run the React-Native application, and I'm trying to figure out how to get the variables that I impose on my intention in the native response code. Is this possible from within native-native, or do I need to write some Java code to get it?

The code I use to run the application:

   Intent intent = new Intent(this, MainActivity.class);
   Intent.putExtra("alarm",true);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);

Thank!

+4
source share
2 answers

Try this to get the Intent parameters in a responsive application.

In my native application, I use this code:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.my.react.app.package");
launchIntent.putExtra("test", "12331");
startActivity(launchIntent);

In a reaction project, my MainActivity.java

public class MainActivity extends ReactActivity {

@Override
protected String getMainComponentName() {
    return "FV";
}

public static class TestActivityDelegate extends ReactActivityDelegate {
    private static final String TEST = "test";
    private Bundle mInitialProps = null;
    private final
    @Nullable
    Activity mActivity;

    public TestActivityDelegate(Activity activity, String mainComponentName) {
        super(activity, mainComponentName);
        this.mActivity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Bundle bundle = mActivity.getIntent().getExtras();
        if (bundle != null && bundle.containsKey(TEST)) {
            mInitialProps = new Bundle();
            mInitialProps.putString(TEST, bundle.getString(TEST));
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected Bundle getLaunchOptions() {
        return mInitialProps;
    }
}

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new TestActivityDelegate(this, getMainComponentName());
  }
}

In my first container, I get a parameter in this.props

export default class App extends Component {

    render() {
        console.log('App props', this.props);

        //...
    }
}

, : http://cmichel.io/how-to-set-initial-props-in-react-native/

+3

startReactApplication, :

Bundle initialProps = new Bundle();
initialProps.putString("alarm", true);

mReactRootView.startReactApplication( mReactInstanceManager, "HelloWorld", initialProps );

. : fooobar.com/questions/555497/...

+1

Source: https://habr.com/ru/post/1653803/


All Articles