设为首页 加入收藏

TOP

Android路由框架-ARouter详解(非原创)(一)
2019-09-01 23:12:46 】 浏览:62
Tags:Android 路由 框架 -ARouter 详解 原创

文章大纲

一、页面路由基本介绍
1.什么是页面路由
2.为什么要使用页面路由
二、页面路由框架ARouter介绍
1.常用功能介绍
2.常见应用场景
三、源码下载
四、参考文章

 

一、页面路由基本介绍

1.什么是页面路由

??映射页面跳转关系,包含跳转相关的URL跳转及值传递、拦截器等功能。

2.为什么要使用页面路由

??在原始android开发中,当我们需要进行页面跳转时,正常写法如下:

Intent intent = new Intent(mContext, XXActivity.class); intent.putExtra("key","value"); startActivity(intent); Intent intent = new Intent(mContext, XXActivity.class); intent.putExtra("key","value"); startActivityForResult(intent, 100); 

上述写法容易出现以下问题:

  1. 多人协同开发的时候,大家都去AndroidManifest.xml中定义各种IntentFilter,使用隐式Intent,最终发现AndroidManifest.xml中充斥着各种Schame,各种Path,需要经常解决Path重叠覆盖、过多的Activity被导出,引发安全风险等问题
  2. 跳转过程中无法插手:直接通过Intent的方式跳转,跳转过程开发者无法干预,一些面向切面的事情难以实施,比方说登录、埋点这种非常通用的逻辑,在每个子页面中判断又很不合理,毕竟activity已经实例化了
  3. 跨模块无法显式依赖:在App小有规模的时候,我们会对App做水平拆分,按照业务拆分成多个子模块,之间完全解耦,通过打包流程控制App功能,这样方便应对大团队多人协作,互相逻辑不干扰,这时候只能依赖隐式Intent跳转,书写麻烦,成功与否难以控制

页面路由可以解决什么问题?

 

 

二、页面路由框架ARouter介绍

1.常用功能介绍

应用内页面跳转

添加依赖

 

温馨提示:api 的版本和 compiler 的版本号需要用最新的。最新的版本在 Github上可以找到。

重写Application并初始化ARouter

 
 

配置将要跳转的页面

 

进行简单的页面跳转

 

 

温馨提示:如果你想实现像 startActivityForResult() 功能,可以这样使用

 

运行结果如下:

 
 

携带参数进行页面跳转

 

温馨提示:支持数据类型如下:

//基础类型 .withString( String key, String value ) .withBoolean( String key, boolean value) .withChar( String key, char value ) .withShort( String key, short value) .withInt( String key, int value) .withLong( String key, long value) .withDouble( String key, double value) .withByte( String key, byte value) .withFloat( String key, float value) .withCharSequence( String key, CharSequence value) //数组类型 .withParcelableArrayList( String key, ArrayList<? extends Parcelable > value) .withStringArrayList( String key, ArrayList<String> value) .withIntegerArrayList( String key, ArrayList<Integer> value) .withSparseParcelableArray( String key, SparseArray<? extends Parcelable> value) .withCharSequenceArrayList( String key, ArrayList<CharSequence> value) .withShortArray( String key, short[] value) .withCharArray( String key, char[] value) .withFloatArray( String key, float[] value) .withCharSequenceArray( String key, CharSequence[] value) //Bundle 类型 .with( Bundle bundle ) //Activity 跳转动画 .withTransition(int enterAnim, int exitAnim) //其他类型 .withParcelable( String key, Parcelable value) .withParcelableArray( String key, Parcelable[] value) .withSerializable( String key, Serializable value) .withByteArray( String key, byte[] value) .withTransition(int enterAnim, int exitAnim) 
 

运行程序,结果如下图所示:

 
 

路由监听
在路由跳转的过程中,我们可以监听路由的过程,只需要以下使用:

navigation(Context context, NavigationCallback callback)

NavigationCallback 的源码如下:

public interface NavigationCallback { /** * Callback when find the destination. * 找到了 * @param postcard meta */ void onFound(Postcard postcard); /** * Callback after lose your way. * 找不到了 * @param postcard meta */ void onLost(Postcard postcard); /** * Callback after navigation. * 跳转完了 * @param postcard meta */ void onArrival(Postcard postcard); /** * Callback on interrupt. * 被拦截了 * @param postcard meta */ void onInterrupt(Postcard postcard); } 

添加用于测试跳转的页面

&nbs
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android屏幕适配讲解与实战(原创) 下一篇Android中资源的引用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目