Android完全組件化—UI跳轉升級改造

獲得Android組件化方案已經開源,參見Android組件化方案開源。方案的解讀文章是一個小的系列,這是系列的第四篇文章:java

  1. Android完全組件化方案實踐
  2. Android完全組件化demo發佈
  3. Android完全組件化-代碼和資源隔離
  4. Android完全組件化—UI跳轉升級改造

自從8月份獲得組件化方案AndroidComponent開源之後,收到了不少朋友的評論或者私信,提出了不少有價值的意見和建議,一個比較集中的點就在於組件之間的UI跳轉不夠優雅。當時因爲業務比較緊張,沒有時間來完善這塊,最近終於抽出時間把UI跳轉的功能進行了升級改造,期間也獲得@leobert等大牛的鼎力支持以及ARouter等優秀方案的啓發,如今DDComponent新的UI跳轉功能已經發布了,歡迎你們使用。android

##已實現功能git

  1. 按組件區分host,增長URL的可讀性
  2. 自動註冊路由,再也不須要手動編寫代碼進行路由分發
  3. 按需加載,只有實際跳轉到某個組件是,該組件的路由表纔會加載
  4. 自動生成路由表文件,方便組件開發團隊之間調用
  5. 參數支持依賴注入,不須要編寫參數的解析代碼

##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

  • host是路由分發的第一級,根據host能夠定位到每一個組件。
  • host還能夠對全部的路由URL進行一個分組,只有調用到該分組的路由的時候,組內的路由纔會被加載進內存

(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。

註冊組件到UIRouter中

在組件的聲明週期類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項目

項目執行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
複製代碼

跳轉

在發起跳轉頁面,有三種方式能夠跳轉到目的頁面

Bundle方式
// 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);
    }
複製代碼
URI方式
// 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&amp;author=";
        legal and illegal data delivering*/
        UIRouter.getInstance().openUri(getActivity(),
                URI_LEGAL
                        + JsonService.Factory.getInstance().create().toJsonString(author), null);
    }
複製代碼
startActivityForResult
//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&amp;author="
                        + JsonService.Factory.getInstance().create().toJsonString(author), null, 7777);
    }
複製代碼

具體使用請參見github源碼

若是對方案有不理解的地方,歡迎閱讀解析文章Android完全組件化方案實踐Android完全組件化demo發佈

目前不少app已經使用ARouter來進行UI跳轉,DDComponent也是支持ARouter的,只是爲了更契合如今的組件化框架,因此本身實現了一套。鑑於已使用ARouter的app遷移成本有點高,後面會專門寫一篇如何在組件化框架中使用ARouter的文章,敬請期待!

相關文章
相關標籤/搜索