ARouter通过路径path反向获取activity

调用标准跳转方法,使得Postcard被实例化并赋值,这样就能拿到相关的数据

private String pathToActivityName(String path) {
        Postcard p = SRouter.getInstance().build(path);
        p.setTag(TestInterceptor.TAG_GET_ACTIVITY);
        p.navigation();
        if (p != null && p.getDestination() != null) {
            LogUtils.showLog("Postcard=" + p.toString());
           //p.getDestination()是一个class,即路径指向的activity
            return p.getDestination().getName();
        }
        return null;
    }

根据tag拦截跳转,为了让Postcard被实例化赋值

@Interceptor(priority = 8)
public class TestInterceptor implements IInterceptor {
    public static final String TAG_GET_ACTIVITY = "tag_get_activity";

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if (postcard.getTag() != null && TextUtils.equals(String.valueOf(postcard.getTag()), TAG_GET_ACTIVITY)) {
            //不做跳转
            callback.onInterrupt(null);
        } else {
            //正常跳转
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
    }
}

你可能感兴趣的:(ARouter通过路径path反向获取activity)