The use of Alibaba's ARouter in Android (interface jump between modules, and data transfer)

1. When developing a relatively large project, you must consider modularization and divide the entire project into n modules. In this way, each module is responsible for a business system, which facilitates later iterative development and makes the project department less bloated. Compiled separately, each module can be run independently. Finally, each module is added to a host APP and merged into a complete project.

After dividing into modules, jumping between interfaces is a big problem. Use Alibaba's ARouter open source library to solve this problem.

In traditional projects, our business is all under one module. Intenter is used to jump between interfaces. This makes the code highly coupled. startActivity(new intenter(testActivity.this,test2Activity.class)); There is also an implicit jump method 

Intent intent = new Intent();

intent.setAction(“com.android.activity.MY_ACTION”);  

startActivity(intent); thisAction 也要在Mainfest.xml注册。也有弊端扩展性很差。

再分模块的项目和一个模块下 界面与界面的跳转  我们都可以用ARouter框架.

2.接下来就在自己的项目中用起来,我这里写一个Demo

Alibaba’s open source routing gitHub address: https://github.com/alibaba/ARouter    

Open its address and add relevant dependencies according to its configuration. Mainly these two places. If the project is synchronized again and no errors are reported, it means the introduction is successful.

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

}
dependencies { 
		
    implementation 'com.alibaba:arouter-api:1.5.0' //The version here is based on the lastest version on github 
    annotationProcessor 'com.alibaba:arouter-compiler:1.2.2' //The version here is based on github The lastest version shall prevail 
}

3. Build a global application to initialize related configurations

public class MyApplication extends Application {
    boolean isDebug=true;
    @Override
    public void onCreate() {
        super.onCreate();
        if (isDebug){   
//这2个必须要在初始化之前开启。These two lines must be written before init, otherwise these configurations will be //invalid in the init process
            ARouter.openLog();
            ARouter.openDebug(); 
 //  Turn on debugging mode (If you are running in InstantRun mode, you must turn on debug mode! Online version //needs to be closed, otherwise there is a security risk)
        }
        ARouter.init(this);   //初始化SDK   As early as possible, it is recommended to initialize in the 
    }
}

4. Create 2 new test activities 

public class MainActivity extends AppCompatActivity {
     Button but_onClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }
    private void initView() {
        but_onClick=(Button) findViewById(R.id.but_onClick);
    }
    private void initEvent() {
                //  点击界面跳转
            but_onClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //1.简单的跳转
        // ARouter.getInstance().build("/test/testActivity").navigation(); //The path here is the annotation address to jump to the interface 
                    //2. Generally, the interface jump must carry the parameter 
                    ARouter .getInstance().build("/test/testActivity") 
                            .withString("userName","Zhang San") 
                            .withInt("age",123) 
                            .navigation(); 
                } 
            }); 
    } 

}

5.This is the interface to jump to

// Add annotations on pages that support routing (required)
// The path here needs to pay attention to need at least two levels : /xx/xx    至少2级
@Route(path = "/test/testActivity")
public class TestActivity extends AppCompatActivity {
    @Autowired
    public String userName;
    @Autowired
   public int age;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ARouter.getInstance().inject(this);   //注入
        Log.d("TAG",userName+age);
    }
}

6. The above is the jump of the interface under a module. It is actually very simple. There is also the jump of Fragment. The routines are the same. Just look at the API it provides.

7. Create a new module, but this module is not an application, but a library. This is the difference between the two. 

apply plugin: 'com.android.application'     apply plugin: 'com.android.library' 

8. Here is a rough picture:

9. My project structure chart:

10. Add 3 module dependencies under the app's build.gradle: baselib, secondtest, testmoudle. As shown in the figure:

11. Here I jump from the host app MainActivity interface to the ThreeActivity interface of baselib:

public class MainActivity extends AppCompatActivity {
     Button but_onClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }
    private void initView() {
        but_onClick=(Button) findViewById(R.id.but_onClick);
    }
    private void initEvent() {
                //  点击界面跳转
            but_onClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //1.简单的跳转
        // ARouter.getInstance().build("/test/testActivity").navigation(); //The path here is the annotation address to jump to the interface 
                    //2. Generally, the interface jump must carry the parameter 
                 ARouter .getInstance().build("/test/demoActivity") 
                            .withString("userName","Zhang San") 
                            .withInt("age",123) 
                            .navigation(); 



                } 
            }); 

    } 

}
@Route(path = "/test/demoActivity")
public class ThreeActivity extends AppCompatActivity {
    Button but_test;
    @Autowired
    public String userName;
    @Autowired
    public int age;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        ARouter.getInstance().inject(this);
        Log.d("TAG",userName+age);

    }

}

12. Interface jumps and data transfer between modules: interface jumps and data transfer are done under the two modules baselib and testmoudle.

@Route(path = "/test/demoActivity")
public class ThreeActivity extends AppCompatActivity {
    Button but_test;
    @Autowired
    public String userName;
    @Autowired
    public int age;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        ARouter.getInstance().inject(this);
        Log.d("TAG",userName+age);
            initView();

    }
    private void initView() {
        but_test=(Button) findViewById(R.id.but_test);
        but_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/testmoudle1/MainActivity")
                        .withString("userName","张三")
                        .withInt("age",123)
                        .navigation();


            }
        });
    }

}
@Route(path = "/testmoudle1/MainActivity")
public class FiveActivity extends AppCompatActivity {
    @Autowired
    public String userName;
    @Autowired
    public int age;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_five);
        ARouter.getInstance().inject(this);
        Log.d("TAG","testmoudle"+userName+age);
    }
}

Summarize:

I introduced the relevant configuration files under the public baselib build.gradle file. And you need to introduce configuration files in other modules. If it is not introduced, an error will be reported. It is a bit unclear. Just these 3 things. Only interface jumps and data transfer are done here. Other operations will be updated next time.

javaCompileOptions {
    annotationProcessorOptions {
        arguments = [AROUTER_MODULE_NAME: project.getName()]
    }
}
implementation 'com.alibaba:arouter-api:1.5.0'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/beautifulYuan/article/details/93192105