- 跨模块跳转Activity
实现这个Activity跳转的路径。
//跳转到我的模块登录页面
public static final String Me_Login = "/me/main/login";
ARouter跳转Activity,在这个Activity上加入注解。
@Route(path = RouteUtils.Me_Login)
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
跳转逻辑是
//登录(跨模块跳转Activity)
ARouter.getInstance().build(RouteUtils.Me_Login).navigation();
- 跨模块实现ForResult返回数据(activity中使用)
实现这个Activity跳转的路径。
//跳转ForResult
public static final String Chat_ForResult = "/chat/main/ForResult";
ARouter跳转Activity,在这个Activity上加入注解。
@Route(path = RouteUtils.Chat_ForResult)
public class ForResultActivity extends AppCompatActivity implements View.OnClickListener {
跳转逻辑是
//跳转ForResult,在fragment中使用不起作用
ARouter.getInstance().build(RouteUtils.Chat_ForResult).navigation(this, 666);
接收数据数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 666:
String name = data.getStringExtra("name");
UIUtils.showToast(name + ",resultCode===>" + resultCode);
break;
default:
break;
}
}
返回数据
Intent intent = new Intent();
intent.putExtra("name", "ForResult返回的数据");
setResult(999, intent);
finish();
- 跳转并携带参数和Eventbus夸模块通信
EventBus.getDefault().register(this);
EventBus.getDefault().unregister(this);
@Subscriber(tag = EvenBusTag.GOTO_EVENTBUS)
public void onEvent(String s) {
UIUtils.showToast(s);
}
//跳转到eventBus数据接收页面
public static final String Me_EventBus = "/me/main/EventBus";
@Route(path = RouteUtils.Me_EventBus)
public class EventBusActivity extends AppCompatActivity implements View.OnClickListener {
// 跳转并携带参数,基本涵盖所以类型传递,具体可以查看Postcard类
//传递过去的值使用getIntent()接收
//在fragment中无法使用ForResult进行夸模块传递数据
//在activity中可以(详细请看LoginActivity)这里的EventBusBean 已经实现了Parcelable接口
EventBusBean eventBusBean = new EventBusBean();
eventBusBean.setProject("android");
eventBusBean.setNum(3);
ARouter.getInstance().build(RouteUtils.Me_EventBus)
.withString("name", "haungxiaoguo")
.withLong("age", 25)
.withParcelable("eventbus", eventBusBean)
.navigation();
@Route(path = RouteUtils.Me_EventBus)
public class EventBusActivity extends AppCompatActivity implements View.OnClickListener {
/**
* eventBus数据接收页面
*/
private TextView mTextView;
/**
* eventBus返回数据
*/
private Button mBtnBackData;
private String name;
private long age;
private EventBusBean eventbus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus);
ARouter.getInstance().inject(this);
initData();
initView();
}
private void initData() {
name = getIntent().getStringExtra("name");
age = getIntent().getLongExtra("age", 0);
eventbus = getIntent().getParcelableExtra("eventbus");
}
private void initView() {
mTextView = (TextView) findViewById(R.id.textView);
mBtnBackData = (Button) findViewById(R.id.btn_back_data);
mBtnBackData.setOnClickListener(this);
mTextView.setText("name=" + name + ",\tage=" + age + ",\tproject=" + eventbus.getProject() +
",\tnum=" + eventbus.getNum());
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.btn_back_data) {
EventBus.getDefault().post(name, EvenBusTag.GOTO_EVENTBUS);
finish();
} else {
}
}
}