1.
android {
compileSdkVersion 28
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
}
dependencies {
...
implementation 'com.alibaba:arouter-api:1.4.0'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.1'
}
2.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// if (isDebug()) {
// 必须写在init之前,否则配置在init过程中无效
// ARouter.openLog();
// ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式 }
// }
ARouter.init(this);
}
private boolean isDebug() {
return true;
}
@Override
public void onTerminate() {
super.onTerminate();
ARouter.getInstance().destroy();
}
}
3.
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ARouter.getInstance().inject(this);
initView();
}
protected abstract void initView();
}
@Route(path = "/app/MainActivity")
public class MainActivity extends BaseActivity {
@Override
protected void initView() {
setContentView(R.layout.activity_main);
}
public void ViewClick(View view) {
ARouter.getInstance().build("/app/OrderActivity").navigation();
}
}
@Route(path = "/app/OrderActivity")
public class OrderActivity extends BaseActivity {
@Override
protected void initView() {
setContentView(R.layout.activity_order);
}
}
4.页面跳转传值:
1.针对于简单页面传值:例如String,int,Long,Object等皆可使用
Person person = new Person();
person.setAge("18");
person.setName("Circle Mei");
ARouter.getInstance().build("/app/OrderActivity")
.withInt("age",123)
.withString("name", "Circle San")
.withParcelable("person",person).navigation();
2.
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name ;
private String age;
public Person(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
protected Person(Parcel in) {
name = in.readString();
age = in.readString();
}
public static final Creator CREATOR = new Creator() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(age);
}
}
?
3.
@Route(path = "/app/OrderActivity")
public class OrderActivity extends BaseActivity {
@Autowired(name = "age")
public int age;
@Autowired(name = "name")
public String name;
@Autowired(name = "person")
public Person person;
@Override
protected void initData() {
//第一种方式:getIntent()
//String name = getIntent().getExtras().getString("name");
//第二种方式:采用注解@Autowired(name = (对应Key))
Toast.makeText(this,person.getName()+" age="+person.getAge(),Toast.LENGTH_LONG).show();
}
@Override
protected void initView() {
setContentView(R.layout.activity_order);
}
?