【问题标题】:React-Native Android - Get the variables from intentReact-Native Android - 从意图中获取变量
【发布时间】:2016-09-06 14:34:03
【问题描述】:

我正在使用 intent 来启动我的 React-Native 应用程序,并且我正在尝试找出如何在 react native 代码中获取我的意图的变量。这可能来自 react-native 还是我必须编写一些 java 代码才能得到它?

我用来启动应用程序的代码:

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

谢谢!

【问题讨论】:

  • 在第二个活动中,getIntent 有什么问题?
  • 第二个活动是 react-native。所以我的问题是如何在 react-native 中获得 getIntent。

标签: android android-intent react-native


【解决方案1】:

试试这个以在 react-native 应用程序中获取 Intent 参数。

在我的原生应用程序中,我使用以下代码:

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

在 react-native 项目中,我的 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());
  }
}

在我的第一个容器中,我得到了 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/

【讨论】:

    【解决方案2】:

    您可以将初始道具作为捆绑传递给startReactApplication 方法中的第三个参数,如下所示:

    Bundle initialProps = new Bundle();
    initialProps.putString("alarm", true);
    
    mReactRootView.startReactApplication( mReactInstanceManager, "HelloWorld", initialProps );
    

    更多详情请查看此答案:https://stackoverflow.com/a/34226172/293280

    【讨论】:

      【解决方案3】:

      根据@Eduardo Junior 的回答,我认为这种方法更正确

      class MainDelegate(activity: ReactActivity, mainComponentName: String?) :
          ReactActivityDelegate(activity, mainComponentName) {
      
          private var params: Bundle? = null
      
          override fun onCreate(savedInstanceState: Bundle?) {
              params = plainActivity.intent.extras
              super.onCreate(savedInstanceState)
          }
      
          override fun onNewIntent(intent: Intent?): Boolean {
              params = intent?.extras
              return super.onNewIntent(intent)
          }
      
          override fun getLaunchOptions() = params
      }
      
      class MainActivity : ReactActivity() {
      
          /**
           * Returns the name of the main component registered from JavaScript. This is used to schedule
           * rendering of the component.
           */
          override fun getMainComponentName() = "example"
      
          override fun createReactActivityDelegate(): ReactActivityDelegate {
              return MainDelegate(this, mainComponentName)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 1970-01-01
        • 2015-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多