ARouter解析二:頁(yè)面跳轉(zhuǎn)源碼分析

在前面中我們對(duì)ARouter的頁(yè)面跳轉(zhuǎn)功能的使用有了基本的了解,由于篇幅的原因沒(méi)有對(duì)跳轉(zhuǎn)的源碼進(jìn)行分析,今天我們就來(lái)探究一下頁(yè)面的跳轉(zhuǎn)過(guò)程。在看這篇文章之前建議小伙伴們先看下面鏈接給出的文章好有個(gè)整體的了解。
ARouter解析一:基本使用及頁(yè)面注冊(cè)源碼解析

整個(gè)流程示意圖如下,接下來(lái)我們會(huì)對(duì)著這個(gè)示意圖開始開車。


頁(yè)面跳轉(zhuǎn)流程.png

1.獲取ARouter實(shí)例

我們先從簡(jiǎn)單的說(shuō)起,不帶參數(shù)的頁(yè)面跳轉(zhuǎn),一行代碼實(shí)現(xiàn)。

ARouter.getInstance().build("/test/activity2").navigation();

用戶基本需要打交道的接口都在ARouter中,該類使用的是單例模式。使用這個(gè)框架的前提就是需要初始化Router,否則會(huì)報(bào)錯(cuò)。

初始化錯(cuò)誤.png

調(diào)用ARouter.init(getApplication());進(jìn)行初始化。單例模式是典型寫法,有兩個(gè)if判斷,第一個(gè)判斷沒(méi)什么可說(shuō)的,之后synchronized上鎖,再進(jìn)行判斷是否null,這個(gè)主要是為了多線程環(huán)境保護(hù)。

public static ARouter getInstance() {
        if (!hasInit) {
            throw new InitException("ARouter::Init::Invoke init(context) first!");
        } else {
            if (instance == null) {
                synchronized (ARouter.class) {
                    if (instance == null) {
                        instance = new ARouter();
                    }
                }
            }
            return instance;
        }
}

2.構(gòu)造路由信息的容器Postcard

得到ARouter實(shí)例后調(diào)用build方法,傳入目標(biāo)頁(yè)面的path("/test/activity2"),我們來(lái)看看build的源碼。這里使用的是代理模式,其實(shí)是調(diào)用_ARouter的build方法,這里需要提的一點(diǎn)是ARouter.init也是調(diào)用的_ARouter的init方法,里面主要是做一些映射文件的加載工作。

public Postcard build(String path) {
        return _ARouter.getInstance().build(path);
}

接著往下看,來(lái)到_ARouter的build方法,注意路徑不能為空,也就是目標(biāo)頁(yè)面必須要有注解@Route(path = "/test/activity2")。然后會(huì)返回一個(gè)Postcard,官方解釋A container that contains the roadmap.這是個(gè)路由信息的存儲(chǔ)器,里面包含頁(yè)面跳轉(zhuǎn)的所有信息。

protected Postcard build(String path) {
        if (TextUtils.isEmpty(path)) {
            throw new HandlerException(Consts.TAG + "Parameter is invalid!");
        } else {
            PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
            if (null != pService) {
                path = pService.forString(path);
            }
            return build(path, extractGroup(path));
        }
}

protected Postcard build(String path, String group) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
            throw new HandlerException(Consts.TAG + "Parameter is invalid!");
        } else {
            PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
            if (null != pService) {
                path = pService.forString(path);
            }
            return new Postcard(path, group);
        }
}

new Postcard(path, group)中,第一個(gè)參數(shù)就是路徑,第二個(gè)參數(shù)是組別信息。那我們的栗子path = "/test/activity2來(lái)說(shuō)test就是group。這里就需要提下,ARouter框架是分組管理,按需加載。提起來(lái)很高深的樣子呢!其實(shí)解釋起來(lái)就是,在編譯期框架掃描了所有的注冊(cè)頁(yè)面/服務(wù)/字段/攔截器等,那么很明顯運(yùn)行期不可能一股腦全部加載進(jìn)來(lái),這樣就太不和諧了。所以就分組來(lái)管理,ARouter在初始化的時(shí)候只會(huì)一次性地加載所有的root結(jié)點(diǎn),而不會(huì)加載任何一個(gè)Group結(jié)點(diǎn),這樣就會(huì)極大地降低初始化時(shí)加載結(jié)點(diǎn)的數(shù)量。比如某些Activity分成一組,組名就叫test,然后在第一次需要加載組內(nèi)的某個(gè)頁(yè)面時(shí)再將test這個(gè)組加載進(jìn)來(lái)。

3.路由信息完善與跳轉(zhuǎn)

ok,我們言歸正傳,就下來(lái)就是一行代碼的最后一個(gè)方法navigation。這里其實(shí)是postcard的navigation方法。

ARouter.getInstance().build("/test/activity2").navigation();

最后會(huì)來(lái)到_ARouter的navigation方法,方法比較長(zhǎng),為了更好的說(shuō)清今天的主題我做了點(diǎn)手腳刪掉一些,不要打我:)我們分成幾個(gè)步驟,第二個(gè)回調(diào)的步驟沒(méi)什么可說(shuō)的,接下來(lái)詳細(xì)解釋下第一和第三步。

1.首先調(diào)用LogisticsCenter.completion完成postcard的補(bǔ)充,這個(gè)詳見后面解析。

2.然后如果有回調(diào)函數(shù)就進(jìn)行回調(diào)。

3.如果需要攔截,就進(jìn)行攔截器的處理,否則就調(diào)用_navigation方法。

protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        try {
            LogisticsCenter.completion(postcard);
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());
            ……
            if (null != callback) {//如果有回調(diào)就進(jìn)行回調(diào)
                callback.onLost(postcard);
            } else {    // No callback for this invoke, then we use the global degrade service.
                DegradeService degradeService = ARouter.getInstance().navigation(DegradeService.class);
                if (null != degradeService) {
                    degradeService.onLost(context, postcard);
                }
            }

            return null;
        }

        if (null != callback) {
            callback.onFound(postcard);
        }
        if (!postcard.isGreenChannel()) { //如果需要攔截
        ……
        } else {//不需要攔截
            return _navigation(context, postcard, requestCode, callback);
        }

        return null;
}

3.1.路由信息完善

postcard我們前面說(shuō)過(guò)是所有路由信息的容器,那么到目前為止我們的postcard中只有path和group的信息,目標(biāo)頁(yè)面是什么還不知道,是不是我吹牛了?別急,LogisticsCenter.completion就是干這個(gè)活的,用來(lái)補(bǔ)充postcard信息的。我們看下源碼,也是比較長(zhǎng)。嘿嘿你猜錯(cuò)了,這個(gè)我就不再做刪減,原生的,我們一步步來(lái)看。

public synchronized static void completion(Postcard postcard) {
        if (null == postcard) {
            throw new NoRouteFoundException(TAG + "No postcard!");
        }

        RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
        if (null == routeMeta) {    // Maybe its does't exist, or didn't load.
            Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup());  // Load route meta.
            if (null == groupMeta) {
                throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]");
            } else {
                // Load route and cache it into memory, then delete from metas.
                try {
                    if (ARouter.debuggable()) {
                        logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] starts loading, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
                    }

                    IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                    iGroupInstance.loadInto(Warehouse.routes);
                    Warehouse.groupsIndex.remove(postcard.getGroup());

                    if (ARouter.debuggable()) {
                        logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] has already been loaded, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
                    }
                } catch (Exception e) {
                    throw new HandlerException(TAG + "Fatal exception when loading group meta. [" + e.getMessage() + "]");
                }

                completion(postcard);   // Reload
            }
        } else {
            postcard.setDestination(routeMeta.getDestination());
            postcard.setType(routeMeta.getType());
            postcard.setPriority(routeMeta.getPriority());
            postcard.setExtra(routeMeta.getExtra());

            Uri rawUri = postcard.getUri();
            if (null != rawUri) {   // Try to set params into bundle.
                Map<String, String> resultMap = TextUtils.splitQueryParameters(rawUri);
                Map<String, Integer> paramsType = routeMeta.getParamsType();

                if (MapUtils.isNotEmpty(paramsType)) {
                    // Set value by its type, just for params which annotation by @Param
                    for (Map.Entry<String, Integer> params : paramsType.entrySet()) {
                        setValue(postcard,
                                params.getValue(),
                                params.getKey(),
                                resultMap.get(params.getKey()));
                    }

                    // Save params name which need autoinject.
                    postcard.getExtras().putStringArray(ARouter.AUTO_INJECT, paramsType.keySet().toArray(new String[]{}));
                }

                // Save raw uri
                postcard.withString(ARouter.RAW_URI, rawUri.toString());
            }

            switch (routeMeta.getType()) {
                case PROVIDER:  // if the route is provider, should find its instance
                    // Its provider, so it must be implememt IProvider
                    Class<? extends IProvider> providerMeta = (Class<? extends IProvider>) routeMeta.getDestination();
                    IProvider instance = Warehouse.providers.get(providerMeta);
                    if (null == instance) { // There's no instance of this provider
                        IProvider provider;
                        try {
                            provider = providerMeta.getConstructor().newInstance();
                            provider.init(mContext);
                            Warehouse.providers.put(providerMeta, provider);
                            instance = provider;
                        } catch (Exception e) {
                            throw new HandlerException("Init provider failed! " + e.getMessage());
                        }
                    }
                    postcard.setProvider(instance);
                    postcard.greenChannel();    // Provider should skip all of interceptors
                    break;
                case FRAGMENT:
                    postcard.greenChannel();    // Fragment needn't interceptors
                default:
                    break;
            }
        }
}

倉(cāng)庫(kù)查找頁(yè)面結(jié)點(diǎn)
首先根據(jù)路徑信息到Warehouse倉(cāng)庫(kù)中查找路由節(jié)點(diǎn)信息,其實(shí)就是幾個(gè)Map,包含有根節(jié)點(diǎn)/攔截器和組別等。

RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
class Warehouse {
    // Cache route and metas
    static Map<String, Class<? extends IRouteGroup>> groupsIndex = new HashMap<>();
    static Map<String, RouteMeta> routes = new HashMap<>();

    // Cache provider
    static Map<Class, IProvider> providers = new HashMap<>();
    static Map<String, RouteMeta> providersIndex = new HashMap<>();

    // Cache interceptor
    static Map<Integer, Class<? extends IInterceptor>> interceptorsIndex = new UniqueKeyTreeMap<>("More than one interceptors use same priority [%s]");
    static List<IInterceptor> interceptors = new ArrayList<>();
}

一開始肯定是沒(méi)有這個(gè)節(jié)點(diǎn)信息的,所以需要到Warehouse.groupsIndex中找到組別的信息,這里就是test.

Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup());  // Load route meta.

然后通過(guò)反射加載這一組類別的映射關(guān)系,就是前面提到的按需加載。然后從倉(cāng)庫(kù)中刪除這個(gè)組別信息節(jié)點(diǎn),防止重復(fù)加載??梢钥匆娋幾g期間已經(jīng)組成了RouteMeta這個(gè)結(jié)點(diǎn)信息,包含有目標(biāo)頁(yè)面,類型,路徑,組別,參數(shù),優(yōu)先級(jí)等信息。

 IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
iGroupInstance.loadInto(Warehouse.routes);
Warehouse.groupsIndex.remove(postcard.getGroup());

我們?cè)倏聪律傻挠成潢P(guān)系文件ARouter$$Group$$test長(zhǎng)什么樣.

ARouter$$Group$$test.png

接下來(lái)會(huì)遞歸調(diào)用completion(postcard),現(xiàn)在routeMeta就不為空了,會(huì)走到else中,首先給postcard補(bǔ)充信息,有了這些信息postcard就可以愉快的工作了。我們這個(gè)栗子中type很明顯是activity,所以就走到default中break出來(lái)了。

3.2. _navigation跳轉(zhuǎn)

繞了一大圈終于要進(jìn)行跳轉(zhuǎn)了aaa!我們來(lái)看下怎么跳轉(zhuǎn)的,可以先猜下,無(wú)法也是startActivity,orz。來(lái)到ACTIVITY分支,從postcard中拿到目標(biāo)頁(yè)面Test2Activity.class然后組成intent,然后putExtras,如果是startActivityForResult,這里面就有參數(shù)。如果context不是activity,那么就需要另起一個(gè)棧Intent.FLAG_ACTIVITY_NEW_TASK進(jìn)行activity的展示。接下來(lái)通過(guò)handler發(fā)送啟動(dòng)activity的任務(wù)。終于找到了熟悉的ActivityCompat.startActivityActivityCompat.startActivityForResult,真是淚流滿面。后面就順理成章了。

private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        final Context currentContext = null == context ? mContext : context;

        switch (postcard.getType()) {
            case ACTIVITY:
                // Build intent
                final Intent intent = new Intent(currentContext, postcard.getDestination());
                intent.putExtras(postcard.getExtras());

                // Set flags.
                int flags = postcard.getFlags();
                if (-1 != flags) {
                    intent.setFlags(flags);
                } else if (!(currentContext instanceof Activity)) {    // Non activity, need less one flag.
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }

                // Navigation in main looper.
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (requestCode > 0) {  // Need start for result
                            ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
                        } else {
                            ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
                        }

                        if ((0 != postcard.getEnterAnim() || 0 != postcard.getExitAnim()) && currentContext instanceof Activity) {    // Old version.
                            ((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
                        }

                        if (null != callback) { // Navigation over.
                            callback.onArrival(postcard);
                        }
                    }
                });

                break;
            case PROVIDER:
                return postcard.getProvider();
            case BOARDCAST:
            case CONTENT_PROVIDER:
            case FRAGMENT:
                Class fragmentMeta = postcard.getDestination();
                try {
                    Object instance = fragmentMeta.getConstructor().newInstance();
                    if (instance instanceof Fragment) {
                        ((Fragment) instance).setArguments(postcard.getExtras());
                    } else if (instance instanceof android.support.v4.app.Fragment) {
                        ((android.support.v4.app.Fragment) instance).setArguments(postcard.getExtras());
                    }

                    return instance;
                } catch (Exception ex) {
                    logger.error(Consts.TAG, "Fetch fragment instance error, " + TextUtils.formatStackTrace(ex.getStackTrace()));
                }
            case METHOD:
            case SERVICE:
            default:
                return null;
        }

        return null;
}

4.總結(jié)

頁(yè)面跳轉(zhuǎn)的源碼基本就是這些內(nèi)容了,分享內(nèi)容只是以頁(yè)面跳轉(zhuǎn)不帶參數(shù)為栗子,其實(shí)帶參數(shù)和頁(yè)面跳轉(zhuǎn)動(dòng)畫設(shè)置都是一樣的,信息都在postcard中,在LogisticsCenter.completion進(jìn)行構(gòu)造,依此類推??梢钥闯稣麄€(gè)框架分層仔細(xì),各個(gè)層之間分工明確。與編譯期間映射關(guān)系打交道的工作都下層到LogisticsCenter,與用戶打交道的API都在ARouter中。學(xué)習(xí)一個(gè)框架最好也可以學(xué)習(xí)下設(shè)計(jì)方法,提升內(nèi)功。

后面當(dāng)然還有解析三,會(huì)分享下url跳轉(zhuǎn)的使用和源碼分析等內(nèi)容,歡迎關(guān)注哦。

你們的贊是我堅(jiān)持的最大動(dòng)力,謝謝!

歡迎關(guān)注公眾號(hào):JueCode

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在前面文章中對(duì)ARouter中頁(yè)面跳轉(zhuǎn)和源碼進(jìn)行了分析,今天我們來(lái)學(xué)習(xí)下通過(guò)URL跳轉(zhuǎn)本地頁(yè)面的使用和跳轉(zhuǎn)源碼分析...
    juexingzhe閱讀 8,103評(píng)論 4 14
  • 開發(fā)一款A(yù)pp,總會(huì)遇到各種各樣的需求和業(yè)務(wù),這時(shí)候選擇一個(gè)簡(jiǎn)單好用的輪子,就可以事半功倍 前言 上面一段代碼,在...
    WangDeFa閱讀 66,068評(píng)論 44 198
  • 本文章用于記錄筆者學(xué)習(xí) ARouter 源碼的過(guò)程,僅供參考,如有錯(cuò)誤之處還望悉心指出,一起交流學(xué)習(xí)。 ARout...
    DevLocke閱讀 14,094評(píng)論 6 52
  • 如果和互相懂得的人在一起,晴天可以逛城市,游風(fēng)景,雨天可以逛書店,看畫展。 如果和相愛的人在一起,晴天可以一起出門...
    靖理閱讀 463評(píng)論 0 3
  • 正如別人所說(shuō),我是個(gè)十足的路癡,在家鄉(xiāng)的地下通道會(huì)出來(lái)不知往哪邊走,夜里的城市是另外一副模樣……我也不知道是什么讓...
    鯉魚夢(mèng)閱讀 385評(píng)論 0 0

友情鏈接更多精彩內(nèi)容