【问题标题】:onNewIntent() is not called on ReactContextBaseJavaModule (react-native)在 ReactContextBaseJavaModule (react-native) 上不调用 onNewIntent()
【发布时间】:2017-08-17 19:58:55
【问题描述】:

我正在构建一个 react native 模块,从我的模块中我发送一个像这样的 PendingIntent。

Intent postAuthorizationIntent = new Intent("com.example.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(reactContext.getApplicationContext(), request.hashCode(), postAuthorizationIntent, 0);

如果我修改 MainActivity,那么我可以在 onNewIntent() 方法中接收数据。这个类看起来像这样......

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "example";
    }

    @Override
    public void onNewIntent(Intent intent) {
        checkIntent(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkIntent(getIntent());
    }

    private void checkIntent(@Nullable Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            switch (action) {
                case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
                    ...
                    break;
                default:
                    // do nothing
            }
        }
    }
}

然后,当我尝试将此逻辑移动到我的模块时,什么也没有发生,它不起作用。

public class ExampleModule extends ReactContextBaseJavaModule implements ActivityEventListener{

    private ReactApplicationContext reactContext;
    private String action = "";

    public ExampleModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.action = "com.example.HANDLE_AUTHORIZATION_RESPONSE";
        reactContext.addActivityEventListener(this);
      }

     @Override
    public String getName() {
        return "ExampleModule";
      }

     @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

      }

      @Override
      public void onNewIntent(Intent intent) {
        checkIntent(intent);
      }

      private void checkIntent(@Nullable Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            switch (action) {
                case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
                    ...
                    break;
                default:
                    // do nothing
            }
        }
    }
}

【问题讨论】:

  • “它不起作用”是什么意思?请提供任何错误消息。
  • 对不起,我的意思是......什么也没发生,onNewIntent 方法从未被调用,我在控制台上看不到任何错误,应用程序继续执行。正如我所说,相同的代码在 MainActivity 中运行良好。

标签: android react-native


【解决方案1】:

我遇到了同样的问题并通过从MainActivity 调用super.onNewIntent(intent) 解决了它:

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
    super.onNewIntent(intent);
}

有了这个,onNewIntent 在你的模块中被调用 - 假设你的模块实现了ActivityEventListener 并且你已经在构造函数中将它注册为一个监听器:

reactContext.addActivityEventListener(this);

【讨论】:

  • 确认有效。似乎我们不能调用super.onNewIntent(intent);,因为没有这样的重载方法。
【解决方案2】:

好的,我解决了这个问题。

public class RNSsfAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener, Application.ActivityLifecycleCallbacks{
    ....

    public ExampleModule(ReactApplicationContext reactContext, Application application) {
            super(reactContext);
            ...
            reactContext.addActivityEventListener(this);
            application.registerActivityLifecycleCallbacks(this);
          }

    ...


    @Override
    public void onActivityStarted(Activity activity) {
        checkIntent(activity.getIntent());
    }

}

【讨论】:

  • 当您将应用程序放入构造函数时,您可以显示您的 Package 类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
相关资源
最近更新 更多