在前面中我們對ARouter的頁面跳轉(zhuǎn)功能的使用有了基本的了解,由于篇幅的原因沒有對跳轉(zhuǎn)的源碼進(jìn)行分析,今天我們就來探究一下頁面的跳轉(zhuǎn)過程。在看這篇文章之前建議小伙伴們先看下面鏈接給出的文章好有個整體的了解。
ARouter解析一:基本使用及頁面注冊源碼解析
整個流程示意圖如下,接下來我們會對著這個示意圖開始開車。

1.獲取ARouter實例
我們先從簡單的說起,不帶參數(shù)的頁面跳轉(zhuǎn),一行代碼實現(xiàn)。
ARouter.getInstance().build("/test/activity2").navigation();
用戶基本需要打交道的接口都在ARouter中,該類使用的是單例模式。使用這個框架的前提就是需要初始化Router,否則會報錯。

調(diào)用
ARouter.init(getApplication());進(jìn)行初始化。單例模式是典型寫法,有兩個if判斷,第一個判斷沒什么可說的,之后synchronized上鎖,再進(jìn)行判斷是否null,這個主要是為了多線程環(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實例后調(diào)用build方法,傳入目標(biāo)頁面的path("/test/activity2"),我們來看看build的源碼。這里使用的是代理模式,其實是調(diào)用_ARouter的build方法,這里需要提的一點是ARouter.init也是調(diào)用的_ARouter的init方法,里面主要是做一些映射文件的加載工作。
public Postcard build(String path) {
return _ARouter.getInstance().build(path);
}
接著往下看,來到_ARouter的build方法,注意路徑不能為空,也就是目標(biāo)頁面必須要有注解@Route(path = "/test/activity2")。然后會返回一個Postcard,官方解釋A container that contains the roadmap.這是個路由信息的存儲器,里面包含頁面跳轉(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)中,第一個參數(shù)就是路徑,第二個參數(shù)是組別信息。那我們的栗子path = "/test/activity2來說test就是group。這里就需要提下,ARouter框架是分組管理,按需加載。提起來很高深的樣子呢!其實解釋起來就是,在編譯期框架掃描了所有的注冊頁面/服務(wù)/字段/攔截器等,那么很明顯運行期不可能一股腦全部加載進(jìn)來,這樣就太不和諧了。所以就分組來管理,ARouter在初始化的時候只會一次性地加載所有的root結(jié)點,而不會加載任何一個Group結(jié)點,這樣就會極大地降低初始化時加載結(jié)點的數(shù)量。比如某些Activity分成一組,組名就叫test,然后在第一次需要加載組內(nèi)的某個頁面時再將test這個組加載進(jìn)來。
3.路由信息完善與跳轉(zhuǎn)
ok,我們言歸正傳,就下來就是一行代碼的最后一個方法navigation。這里其實是postcard的navigation方法。
ARouter.getInstance().build("/test/activity2").navigation();
最后會來到_ARouter的navigation方法,方法比較長,為了更好的說清今天的主題我做了點手腳刪掉一些,不要打我:)我們分成幾個步驟,第二個回調(diào)的步驟沒什么可說的,接下來詳細(xì)解釋下第一和第三步。
1.首先調(diào)用LogisticsCenter.completion完成postcard的補(bǔ)充,這個詳見后面解析。
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我們前面說過是所有路由信息的容器,那么到目前為止我們的postcard中只有path和group的信息,目標(biāo)頁面是什么還不知道,是不是我吹牛了?別急,LogisticsCenter.completion就是干這個活的,用來補(bǔ)充postcard信息的。我們看下源碼,也是比較長。嘿嘿你猜錯了,這個我就不再做刪減,原生的,我們一步步來看。
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;
}
}
}
倉庫查找頁面結(jié)點
首先根據(jù)路徑信息到Warehouse倉庫中查找路由節(jié)點信息,其實就是幾個Map,包含有根節(jié)點/攔截器和組別等。
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<>();
}
一開始肯定是沒有這個節(jié)點信息的,所以需要到Warehouse.groupsIndex中找到組別的信息,這里就是test.
Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup()); // Load route meta.
然后通過反射加載這一組類別的映射關(guān)系,就是前面提到的按需加載。然后從倉庫中刪除這個組別信息節(jié)點,防止重復(fù)加載。可以看見編譯期間已經(jīng)組成了RouteMeta這個結(jié)點信息,包含有目標(biāo)頁面,類型,路徑,組別,參數(shù),優(yōu)先級等信息。
IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
iGroupInstance.loadInto(Warehouse.routes);
Warehouse.groupsIndex.remove(postcard.getGroup());
我們再看下生成的映射關(guān)系文件ARouter$$Group$$test長什么樣.

接下來會遞歸調(diào)用completion(postcard),現(xiàn)在routeMeta就不為空了,會走到else中,首先給postcard補(bǔ)充信息,有了這些信息postcard就可以愉快的工作了。我們這個栗子中type很明顯是activity,所以就走到default中break出來了。
3.2. _navigation跳轉(zhuǎn)
繞了一大圈終于要進(jìn)行跳轉(zhuǎn)了aaa!我們來看下怎么跳轉(zhuǎn)的,可以先猜下,無法也是startActivity,orz。來到ACTIVITY分支,從postcard中拿到目標(biāo)頁面Test2Activity.class然后組成intent,然后putExtras,如果是startActivityForResult,這里面就有參數(shù)。如果context不是activity,那么就需要另起一個棧Intent.FLAG_ACTIVITY_NEW_TASK進(jìn)行activity的展示。接下來通過handler發(fā)送啟動activity的任務(wù)。終于找到了熟悉的ActivityCompat.startActivity和ActivityCompat.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é)
頁面跳轉(zhuǎn)的源碼基本就是這些內(nèi)容了,分享內(nèi)容只是以頁面跳轉(zhuǎn)不帶參數(shù)為栗子,其實帶參數(shù)和頁面跳轉(zhuǎn)動畫設(shè)置都是一樣的,信息都在postcard中,在LogisticsCenter.completion進(jìn)行構(gòu)造,依此類推??梢钥闯稣麄€框架分層仔細(xì),各個層之間分工明確。與編譯期間映射關(guān)系打交道的工作都下層到LogisticsCenter,與用戶打交道的API都在ARouter中。學(xué)習(xí)一個框架最好也可以學(xué)習(xí)下設(shè)計方法,提升內(nèi)功。
后面當(dāng)然還有解析三,會分享下url跳轉(zhuǎn)的使用和源碼分析等內(nèi)容,歡迎關(guān)注哦。
你們的贊是我堅持的最大動力,謝謝!
歡迎關(guān)注公眾號:JueCode