Android 页面跳转 PageRouter

作者: 半只温柔 | 来源:发表于2019-08-04 19:22 被阅读60次

    项目 demo 地址 用apt 实现欢迎 star issues

    注解 @Route @Args
    不用关心所跳转页面所需参数,BundleBuilder 会根据 @Args 获取变量名并自动生成 set 方法,输入 Test1ActivityBundle.Builder(). 后会提示出页面参数,省了撸代码找参数的时间

    调用
    PageRouter
              .of(this)
              .path(Constants.RouterPath.TEST2)
              .navigation();
    
    PageRouter
              .of(this)
              .path(Constants.RouterPath.TEST1)
              .setParams(new Test1ActivityBundle.Builder()
                                .setName('z')
                                .setMoney(50000)
                                .setBeanS(new SerializableBean("SerializableBean"))
                                .setBeanP(new ParcelableBean("ParcelableBean"))
                                .setStringArrayList(as)
                                .setIntegerArrayList(ai)
                                .setBm(bm)
                                .setMsg("Welcome2Test1Activity")
                                .bundle())
              .setUri(Uri.parse(""))
              .navigation();
    
    注解及其使用
    @Route(path = Constants.RouterPath.TEST1)
    public class Test1Activity extends AppCompatActivity {
        @Args
        public String msg;
        @Args
        public Character name;
        @Args
        public double money;
        @Args
        public SerializableBean beanS;
        @Args
        public ParcelableBean beanP;
        @Args
        public Bitmap bm;
        @Args
        public ArrayList<String> stringArrayList;
        @Args
        public ArrayList<Integer> integerArrayList;
    ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_1);
            // 引入
            BundleService.bind(this);
    

    demo gif :


    router.gif

    原理:(都在生成的文件下)
    也就是大家熟悉的 apt 技术了
    生产的代码 segment 如下 👇
    RouterDispatcher.java
    naviagtion() 方法 会调用 go() 方法

    public static void go(Context ctx,String path,Bundle extras, Uri uri){ 
            if(ctx==null)return;
            Intent intent = null;
            switch (path){
                case "Test2Activity":
                   intent = new Intent(ctx, Test2Activity.class);
                break;
                case "Test1Activity":
                   intent = new Intent(ctx, Test1Activity.class);
                break;
    
            }
            if (intent == null) return;
            if (extras != null) {
                intent.putExtras(extras);
            }
            if (!(ctx instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            if (uri != null){
                intent.setData(uri);
            }
            ctx.startActivity(intent);
    
        }
    

    Test1ActivityBundle.java
    BundleService.bind 会调用 bind 方法

    ... 省略部分代码
    public Test1ActivityBundle.Builder setMsg(java.lang.String msg){
        args.putString("msg",msg);
        return this;
    }
    public Test1ActivityBundle.Builder setName(java.lang.Character name){
        args.putChar("name",name);
        return this;
    }
    ... 省略部分代码
    public static void bind(Test1Activity target) {
            Intent intent = target.getIntent();
            if (intent==null)return;
            Bundle source = intent.getExtras();
            if (source==null)return;
            if (source.containsKey("msg")) {
                target.msg = (java.lang.String) source.getString("msg");
            } else {
                throw new IllegalStateException("msg is required, but not found in the bundle.");
            }
            if (source.containsKey("name")) {
                target.name = (java.lang.Character) source.getChar("name");
            } else {
                throw new IllegalStateException("name is required, but not found in the bundle.");
            }
    ... 省略部分代码
    

    相关文章

      网友评论

        本文标题:Android 页面跳转 PageRouter

        本文链接:https://www.haomeiwen.com/subject/atwydctx.html