[Alibaba-ARouter]Android页面路由框架简单使用

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<Person> CREATOR = new Creator<Person>() {
        @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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值