ARouter实现界面跳转和传参

背景:ARouter字面意思一个路由器,是由阿里巴巴集团开源的路由框架,主要解决不同module之间界面跳转的问题,现在大型app都流行模块化开发,比如登录功能写成一个module,设置功能写成一个module,主页写成一个module,这样界面间的跳转使用传统的方式会非常复杂,于是ARouter诞生了!

ARouter实现界面跳转和传参_第1张图片

原理:
所有的module都引用ARouter,module生成一个映射表传到ARouter中,实现module之间不互相依赖但可以显时跳转。
https://gitee.com/mirrors/ARouter?_from=gitee_search

1添加依赖

javaCompileOptions {

            annotationProcessorOptions {

                arguments = [moduleName :project.getName() ]

            } }
    compile'com.alibaba:arouter-api:1.3.1'

    annotationProcessor'com.alibaba:arouter-compiler:1.1.4'

ARouter实现界面跳转和传参_第2张图片
2起始activity

package com.example.arouter;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.myapplication.R;
@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);
    }

    public void go(View view) {
        ARouter.getInstance().build(Constance.DeatailActivityURL)
                .withString("name","wy")
                .withInt("age",6)
                .navigation();

    }
}

3目标activity

package com.example.arouter;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.myapplication.R;
@Route(path = Constance.DeatailActivityURL)
public class ArouterDetailActivity extends AppCompatActivity {


    @Autowired
    public String name;
    @Autowired
    public int age;

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

4url池塘

package com.example.arouter;

public class Constance {
    public static final String DeatailActivityURL="/app/ArouterDetailActivity";
}

你可能感兴趣的:(android)