Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter页面启动 Activity页面,如何拿到参数? #18

Closed
ruanchaohouzi opened this issue Mar 13, 2019 · 10 comments
Closed

Flutter页面启动 Activity页面,如何拿到参数? #18

ruanchaohouzi opened this issue Mar 13, 2019 · 10 comments

Comments

@ruanchaohouzi
Copy link

Flutter页面代码:
FlutterBoost.singleton.openPage("pagename", {"key":"value"});
Android代码
FlutterBoostPlugin.init(new IPlatform() { ........... /** * start a new activity from flutter page, you may need a activity router. */ @Override public boolean startActivity(Context context, String url, int requestCode) { return PageRouter.openPageByUrl(context, url, requestCode); } .......... });

Flutter启动Android Activity,请问下Android代码 startActivity(Context context, String url, int requestCode)怎么拿到Flutter中传过来的参数key,value????

@nightwolf-chen
Copy link
Contributor

安卓会把参数拼接在url尾部

@ruanchaohouzi
Copy link
Author

安卓会把参数拼接在url尾部

@nightwolf-chen Android实际测试发现并没有拼在url尾部
Flutter调用端代码
FlutterBoost.singleton.openPage("pagename", {"name":"ruanchao"});
实际调试Android端拿到的url截图
image

@nightwolf-chen
Copy link
Contributor

我们在实现的过程中在路由层将url参数进行了拼接。所以建议你自己实现一个路由层,处理参数传递。

@ruanchaohouzi
Copy link
Author

我们在实现的过程中在路由层将url参数进行了拼接。所以建议你自己实现一个路由层,处理参数传递。

@nightwolf-chen
Future openPage(String url, Map params,
{bool animated, PageResultHandler resultHandler}) {
return _router.openPage(url, params,
animated: animated, resultHandler: resultHandler);
}
参数直接拼接在url后面,那在Android端你这个接口设计的有问题啊?Map params这个应该是可选参数啊,在Android端其实设置params是无效的参数

@nightwolf-chen
Copy link
Contributor

这里是有点不合理,我们后面会重新考虑这个问题。如果你有好的修复欢迎提pull request。

@Yacumima
Copy link
Collaborator

Flutter页面代码:
FlutterBoost.singleton.openPage("pagename", {"key":"value"});
Android代码
FlutterBoostPlugin.init(new IPlatform() { ........... /** * start a new activity from flutter page, you may need a activity router. */ @Override public boolean startActivity(Context context, String url, int requestCode) { return PageRouter.openPageByUrl(context, url, requestCode); } .......... });

Flutter启动Android Activity,请问下Android代码 startActivity(Context context, String url, int requestCode)怎么拿到Flutter中传过来的参数key,value????

======
嗯,这是我们的一个不合理的地方,也是为了兼容以前老的逻辑,对params的格式是有默认要求的,
这个是解析的地方:
public class NavigationService_openPage implements MessageHandler{
...
private boolean onCall(MessageResult result,String pageName,Map params,Boolean animated){
Map pageParams = null;
int requestCode = 0;

     if(params != null && params.get("query") != null) {
         pageParams = (Map)params.get("query");
     }

     if(params != null && params.get("requestCode") != null) {
         requestCode = (int)params.get("requestCode");
     }

     FlutterBoostPlugin.openPage(null,pageName,pageParams,requestCode);

     result.success(true);
     return true;
  }

}

可见,带给page的参数必须是放在query字段下的一个map

@heluotian
Copy link

Android 需要在外面包一层
int requestCode = params["requestCode"];
Map nativeParams = {"query":params,"requestCode":requestCode};
FlutterBoost.singleton.openPage(url, nativeParams, animated: animated,resultHandler: resultHandler);

@kangshifu
Copy link

原生的activty怎么取这个params?

@Yacumima
Copy link
Collaborator

Yacumima commented Mar 25, 2019

原生的activty怎么取这个params?

会自动拼接到startActivity url的query参数中

@zhaochengxiang
Copy link

flutter中openPage中params 必须类似于{"query":{"present":true}} 这样Activity中就能在url中解析

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants