獲得Android組件化方案已經開源,參見Android組件化方案開源。方案的解讀文章是一個小的系列,這是系列的第四篇文章:java
自從8月份獲得組件化方案AndroidComponent開源之後,收到了不少朋友的評論或者私信,提出了不少有價值的意見和建議,一個比較集中的點就在於組件之間的UI跳轉不夠優雅。當時因爲業務比較緊張,沒有時間來完善這塊,最近終於抽出時間把UI跳轉的功能進行了升級改造,期間也獲得@leobert等大牛的鼎力支持以及ARouter等優秀方案的啓發,如今DDComponent新的UI跳轉功能已經發布了,歡迎你們使用。android
##已實現功能git
##URL結構 組件之間的UI跳轉是基於標準的URL來實現的,先簡單看一下URL的基本構成:github
<scheme>://<host>/<path>?<query>
複製代碼
這個是最簡單的一個模型,不過用於咱們UI跳轉協議已經足夠了。下面是一個跳轉到分享頁面的實例:bash
DDComp://share/shareBook?bookName=Gone with the Wind
複製代碼
結合咱們的組件化框架,咱們簡單講一下各部分的含義app
(1)scheme對應的是DDComp,這個在DDComponent沒有限制,使用時能夠本身自由設置,通常爲了從應用外跳轉進來,會在manifest的入口Activity添加如下配置框架
<intent-filter>
<data android:scheme="DDComp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
複製代碼
(2)host對應的是share。在組件化框架中,每一個組件對應一個惟一的host,例如分享組件的host就是share,讀書組件的host是reader等等。ide
(3)path對應的是shareBook。它對應的是具體的每一個具體的頁面,例如shareBook對應就是ShareActivity。在一個組件以內,path是不能重複的組件化
(4)query對應的是bookName=Gone with the Wind。它表示要跳轉到ShareActivity,須要傳入的參數,例如這裏須要傳入書的名字等。gradle
下面咱們就講一下如何使用DDComponent的UI跳轉新功能!
在組件的build.gradle中添加依賴
annotationProcessor 'com.luojilab.ddcomponent:router-anno-compiler:1.0.0'
複製代碼
同時添加
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [host: "share"]
}
}
}
複製代碼
此處的"share"是跳轉URI中的host,每一個組件須要設置不一樣的host。
在組件的聲明週期類ApplicationLike中,添加註冊和反註冊代碼
public class ShareApplike implements IApplicationLike {
UIRouter uiRouter = UIRouter.getInstance();
@Override
public void onCreate() {
uiRouter.registerUI("share");
}
@Override
public void onStop() {
uiRouter.unregisterUI("share");
}
}
複製代碼
首先在跳轉的目的Activity上添加RouteNode註解
@RouteNode(path = "/shareBook", desc = "分享書籍頁面")
public class ShareActivity extends AppCompatActivity {
複製代碼
若是須要傳入參數,在具體的參數定義上增長Autowired註解:
@Autowired
String bookName;
@Autowired
Author author;
複製代碼
注意此處的參數不能是private的,不然編譯會直接報錯。這裏的緣由在於依賴注入的時候沒有使用反射,而是直接調用了該參數,因此須要參數至少是包可見的。
若是想使用自動裝載功能,須要在Activity的onCreate中調用方法
AutowiredService.Factory.getInstance().create().autowire(this);
複製代碼
建議該方法在基類Activity中調用
項目執行build,會生成apt文件,具體可在build目錄下面查看 同時還會在根目錄生成UIRouterTable文件夾,裏面會列出每一個組件向外提供的路由表
auto generated, do not change !!!!
HOST : share
分享雜誌頁面
/shareMagazine
author:com.luojilab.componentservice.share.bean.Author
bookName:String
分享書籍頁面
/shareBook
author:com.luojilab.componentservice.share.bean.Author
bookName:String
複製代碼
在發起跳轉頁面,有三種方式能夠跳轉到目的頁面
// UI transfer with Bundle
private void goToShareActivityWithBundle() {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
Bundle bundle = new Bundle();
bundle.putString("bookName", "Gone with the Wind");
bundle.putString("author", JsonService.Factory.getInstance()
.create().toJsonString(author));
UIRouter.getInstance().openUri(getActivity(), "DDComp://share/shareBook", bundle);
}
複製代碼
// UI transfer with URI
private void goToShareActivityWithUri() {
Author author = new Author();
author.setName("Barack Obama");
author.setCounty("New York");
final String URI_LEGAL = "DDComp://share/shareMagazine?bookName=NYTIME&author=";
legal and illegal data delivering*/
UIRouter.getInstance().openUri(getActivity(),
URI_LEGAL
+ JsonService.Factory.getInstance().create().toJsonString(author), null);
}
複製代碼
//startActivityForResult
private void goToShareActivityForResult() {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
UIRouter.getInstance().openUri(getActivity(),
"DDComp://share/shareBook?bookName=Gone with the Wind&author="
+ JsonService.Factory.getInstance().create().toJsonString(author), null, 7777);
}
複製代碼
具體使用請參見github源碼。
若是對方案有不理解的地方,歡迎閱讀解析文章Android完全組件化方案實踐和Android完全組件化demo發佈
目前不少app已經使用ARouter來進行UI跳轉,DDComponent也是支持ARouter的,只是爲了更契合如今的組件化框架,因此本身實現了一套。鑑於已使用ARouter的app遷移成本有點高,後面會專門寫一篇如何在組件化框架中使用ARouter的文章,敬請期待!