ARouter使用(管用)

背景:
原生的路由(跳转)方案存在的问题:
显示跳转:类依赖耦合严重
隐士跳转:规则集中化管理,协作扩展困难
ARouter由此诞生!
使用:
1 在app的build.gradle中的defaultConfig中

javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }


dependencies中

 implementation 'com.alibaba:arouter-api:1.5.2'
 annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'

在这里插入图片描述
2项目的application中

public class WyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if (isDebug()) {           // 这两行必须写在init之前,否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(WyApp.this); // 尽可能早,推荐在Application中初始化
    }

    private boolean isDebug() {
        return true;
    }
}

3MainActivity中(实现跳转到另一个activity)

 public void go(View view) {
        ARouter.getInstance().build("/app/ArouterActivity").navigation();
    }

4ArouterActivity

@Route(path = "/app/ArouterActivity")
public class ArouterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arouter);
        ARouter.getInstance().inject(this);
    }
}

5 Activity之间携带参数跳转(string)

 ARouter.getInstance().build("/app/ArouterActivity")
                .withString("wy","I am from MainActivity data!")//携带参数跳转
                .navigation();
  @Autowired
    String wy;
  Log.e("wy", "onCreate: "+wy);
@Route(path = "/app/ArouterActivity")
public class ArouterActivity extends AppCompatActivity {
    @Autowired
    String wy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arouter);
        ARouter.getInstance().inject(this);
        Log.e("wy", "onCreate: "+wy);
    }
}

6 Activity之间携带参数跳转(自定义对象)

   ARouter.getInstance().build("/app/ArouterActivity")
                .withString("wy","I am from MainActivity data!")//携带参数跳转
                .withSerializable("user",new User("xiaoming",18))
                .navigation();

第二个属性写成
@Autowired
User user;
也行,相当于
@Autowired(name=“user”)
User user;

@Route(path = "/app/ArouterActivity")
public class ArouterActivity extends AppCompatActivity {
    @Autowired
    String wy;

    @Autowired(name="user")
    User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arouter);
        ARouter.getInstance().inject(this);
        Log.e("wy", "onCreate: "+wy+" user: "+mUser);
    }
}
public class User implements Serializable {
    public String name;
    public int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在这里插入图片描述
7拦截器的使用:经验:如果拦截器没起作用,卸载重装apk
定义两个拦截器FirstInterceptor,SecondInterceptor ,priority数字越小越选执行

@Interceptor(priority = 1)
public class FirstInterceptor implements IInterceptor {
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        Log.e("wy", "FirstInterceptor  getPath:" +postcard.getPath()+ " callback:"+callback.toString() );
        callback.onContinue(postcard);

    }

    @Override
    public void init(Context context) {
        Log.e("wy", "FirstInterceptor init: "+context.toString() );

    }
}
@Interceptor(priority = 2)
public class SecondInterceptor implements IInterceptor {
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        Log.e("wy", "SecondInterceptor process: "+postcard.getPath()+ " callback:"+callback.toString() );
        callback.onContinue(postcard);

//        if ("/app/ArouterActivity".equals(postcard.getPath())) {
//
//            // 这里的弹窗仅做举例,代码写法不具有可参考价值
//            final AlertDialog.Builder ab = new AlertDialog.Builder(postcard.getContext());
//            ab.setCancelable(false);
//            ab.setTitle("温馨提醒");
//            ab.setMessage("想要跳转到Test4Activity么?(触发了\"/inter/test1\"拦截器,拦截了本次跳转)");
//            ab.setNegativeButton("继续", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    callback.onContinue(postcard);
//                }
//            });
//            ab.setNeutralButton("算了", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    callback.onInterrupt(null);
//                }
//            });
//            ab.setPositiveButton("加点料", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    postcard.withString("extra", "我是在拦截器中附加的参数");
//                    callback.onContinue(postcard);
//                }
//            });
//
//            ab.create().show();
//
            MainLooper.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ab.create().show();
                }
            });
//        } else {
//            callback.onContinue(postcard);
//        }

    }

    @Override
    public void init(Context context) {
        Log.e("wy", "SecondInterceptor init: "+context.toString() );

    }
}

看运行日志
在这里插入图片描述
7.1在拦截器中添加参数:参见加点料,原生的跳转方案做不到这一点,只能直接跳转,无法拦截干预!

@Interceptor(priority = 1)
public class FirstInterceptor implements IInterceptor {
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        Log.e("wy", "FirstInterceptor process getPath:" +postcard.getPath()+ " callback:"+callback.toString() );
//        callback.onContinue(postcard);
        if ("/app/ArouterActivity".equals(postcard.getPath())) {

            // 这里的弹窗仅做举例,代码写法不具有可参考价值
            final AlertDialog.Builder ab = new AlertDialog.Builder(postcard.getContext());
            ab.setCancelable(false);
            ab.setTitle("温馨提醒");
            ab.setMessage("想要跳转到ArouterActivity么?(触发了\"FirstInterceptor\"拦截器,拦截了本次跳转)");
            ab.setNegativeButton("继续", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    callback.onContinue(postcard);
                }
            });
            ab.setNeutralButton("算了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    callback.onInterrupt(null);
                }
            });
            ab.setPositiveButton("加点料", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    postcard.withString("extra", "我是在拦截器中附加的参数");
                    callback.onContinue(postcard);
                }
            });


            MainLooper.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ab.create().show();
                }
            });
        } else {
            callback.onContinue(postcard);
        }

    }

    @Override
    public void init(Context context) {
        Log.e("wy", "FirstInterceptor init: "+context.toString() );

    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/121333864
今日推荐