didi / DRouter

Android Router Framework
Apache License 2.0
745 stars 93 forks source link

路由到Fragment是否可以用非回调式? #27

Closed anymao closed 2 years ago

anymao commented 2 years ago

看了wiki示例,对于路由到Fragment这节: https://github.com/didi/DRouter/wiki/1.-Router#fragment%E5%AF%BC%E8%88%AA 是否存在同步式路由带Fragment的调用方式?类似于 Fragment f = DRouter.build("didi://router/message").start();

在像ViewPager+DRouter这种场景的时候,需要获取到多个Fragment再设置到Adapter中,会有这种需求。

tks!

weigao123 commented 2 years ago

我会重新考虑一下,不过目前有同步方式获取到Fragment,参考以下

protected @NonNull Fragment newFragment(String uri) {
    final Fragment[] fragments = {null};
    DRouter.build(uri).start(null, new RouterCallback() {
        @Override
        public void onResult(@NonNull Result result) {
            fragments[0] = result.getFragment();
        }
    });
    if (fragments[0] == null) {
        return new EmptyFragment();
    }
    return fragments[0];
}