首页 > 解决方案 > React Native Android Intent putExtra obj ReadableMap 如何恢复对象

问题描述

我有以下代码:

ReadableMap infoIntent =  frame.getMap("infoIntent");
Log.v("RN2: ", "/uri: " + infoIntent);

intent appIntent = new Intent(mReactContext, getCurrentActivity().getClass());
appIntent.setAction(ACTION_SHORTCUT);
appIntent.putExtra(SHORTCUT_ITEM, infoIntent);//(Parcelable) ??

我必须确保恢复这些数据并将其传递给承诺的人,所以我感觉是这样的:

    @ReactMethod
    @TargetApi(25)
    public void popInitialAction(Promise promise) {
        try {
            Activity currentActivity = getCurrentActivity();
            WritableMap map = null;

            if (currentActivity != null) {
                Intent intent = currentActivity.getIntent();

                if (ACTION_SHORTCUT.equals(intent.getAction())) {
                    Parcelable bundle = intent.getParcelableExtra(SHORTCUT_ITEM);
                    Log.v("RN2: ", "/uri2: " + bundle);
                    /*if (bundle != null) {
                        map = item.toWritableMap();
                    }*/
                }
            }

            promise.resolve(map);
        } catch (Exception e) {
            promise.reject(new JSApplicationIllegalArgumentException(
                    "AppShortcuts.popInitialAction error. " + e.getMessage()));
        }
    }

对象的类型是这样的:

infoIntent: {
      name: "Megan",
      surname: "Fox",
      urlImg:
        'https://scontent-mxp1-1.cdninstagram.com/vp/3c4732c2cd3566727dad10f03c04b7bd/5C9241C4/t51.2885-19/s150x150/34706107_1875460276079648_8096847319644766208_n.jpg',
      age: 32,
      height: "1.63 m"
    }

对象的特性可能会有所不同,所以我不想创建一个带有 set 和 get 参数名称的类。

标签: javaandroidreact-nativeandroid-intentpromise

解决方案


推荐阅读