映射页面跳转关系,包含跳转相关的URL跳转及值传递、拦截器等功能。
在原始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);
上述写法容易出现以下问题:
应用内页面跳转
添加依赖
温馨提示:api 的版本和 compiler 的版本号需要用最新的。最新的版本在 Github上可以找到。
重写Application并初始化ARouter 配置将要跳转的页面
进行简单的页面跳转
温馨提示:支持数据类型如下: //基础类型 .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)
ARouter.getInstance().inject(this);
我们有一个singletask启动模式的activity,在onNewIntent方法中调用ARouter.getInstance().inject(this);
得不到参数,查看ARouter在build过程中生成的代码可以知道它是调用了activity的getIntent来获取参数的,但是onNewIntent中的intent和在onCreate方法中的intent并不相同,所以需要在onNewIntent方法中调用setIntent方法,然后就能得到参数了。
ARouter::Compiler >>> No module name, for more information, look at gradle log.
检查项目依赖的全部module包括module依赖的module(没有页面的module也算),在每个module的 build.gradle中加上下面的代码。
defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments = [ AROUTER_MODULE_NAME : project.getName() ] } } }
不同module的一级路径必须不同,否则会导致一个moudle中的一级路径失效
下面是抛出异常的源代码
public synchronized static void completion(Postcard postcard) { if (null == postcard) { throw new NoRouteFoundException(TAG + "No postcard!"); } //查找RouteMeta对象,如果存在说明路由成功,如果失败说明还没有被加载或者这个path就是错误的 RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath()); if (null == routeMeta) { // 通过groupsIndex去找IRouteGroup的实现类 Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup()); if (null == groupMeta) { throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]"); } else { // 通过反射获取IRouteGroup的实现类,然后加载到内存 IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance(); iGroupInstance.loadInto(Warehouse.routes); // 加载到内存后Warehouse.groupsIndex去掉这个group Warehouse.groupsIndex.remove(postcard.getGroup()); } } else { //查找到路由地址 。。。。。 } }
by:yzl
原文:https://www.cnblogs.com/widgetbox/p/yzl.html