app内各个module之间跳转
程序员文章站
2022-06-08 18:26:54
...
当我们在开发的过程中有时候有些类或者基类背封装成lib,从lib库中类跳转到app module类中使用intent是不行的,今天介绍下两种实现方式
1、使用第三方开源框架(阿里巴巴的Router)
1)在build.gradle中添加依赖:
defaultConfig中添加:
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName :project.getName() ]
} }
依赖库:
compile'com.alibaba:arouter-api:1.3.1'
annotationProcessor'com.alibaba:arouter-compiler:1.1.4'
2)在Application中进行初始化
3)使用:
(1)首先:在Activity/Fragment类上面写上 Route path 注解。
注意:这里的路径需要注意的是至少需要有两级,/xx/xx
(2)然后:在接收的Activity类里写上路由注解头部;
/xx/xx 即和跳转的注解一样
(3)接着:目标的Activity类上面需要声明Route path 注解,以此对应(跳转如果不对应路径,框架会Toast说路径不匹配)
2、自己编写代理类
1)在lib模块中写一个接口类并定义好接口方法,
/**
* @uthor:created by Hunter2916
* 时间:2019/10/12 15:05
* 描述:静态代理类接口
**/
public interface IActivityManagerJump {
void startRewardAnchorActivity(Context context, String title, String url, int id);
void startFollwDetailActivity(Context context, int starId);
void startLoginActivityActivity(Context context);
void startWebviewActivity(Context context, String url, String title);
void startVideoActivity(Context context, int vid);
void startChallengeDetailActivity(Context context, int cid, String challengeName);
}
2)在lib模块中编写一个实现类,并定义好单例调用方法以及初试化方法init()
/**
* @uthor:created by Hunter2916
* 时间:2019/10/12 15:02
* 描述:跨module跳转 代理类
**/
public class ActivityManagerJumpImp implements IActivityManagerJump {
private static volatile ActivityManagerJumpImp instance;
private IActivityManagerJump activityManagerJump;
//单列模式
public static ActivityManagerJumpImp getIntance() {
if (instance == null) {
synchronized (ActivityManagerJumpImp.class) {
if (instance == null) {
instance = new ActivityManagerJumpImp();
}
}
}
return instance;
}
public void init(IActivityManagerJump activityManagerJump) {
this.activityManagerJump = activityManagerJump;
}
@Override
public void startRewardAnchorActivity(Context context, String title, String url, int id) {
activityManagerJump.startRewardAnchorActivity(context, title, url, id);
}
@Override
public void startFollwDetailActivity(Context context, int starId) {
activityManagerJump.startFollwDetailActivity(context, starId);
}
@Override
public void startLoginActivityActivity(Context context) {
activityManagerJump.startLoginActivityActivity(context);
}
@Override
public void startWebviewActivity(Context context, String url, String title) {
activityManagerJump.startWebviewActivity(context, url, title);
}
@Override
public void startVideoActivity(Context context, int vid) {
activityManagerJump.startVideoActivity(context, vid);
}
@Override
public void startChallengeDetailActivity(Context context, int cid, String challengeName) {
activityManagerJump.startChallengeDetailActivity(context, cid, challengeName);
}
}
3)在App模块中写个实现类去实现IActivityManagerJump接口类,并且真正实现其方法
/**
* @uthor:created by Hunter2916
* 时间:2019/10/12 15:02
* 描述:真正执行类
**/
public class ActivityManagerJumpActivity implements IActivityManagerJump {
@Override
public void startRewardAnchorActivity(Context context, String title, String url, int id) {
String titleLocal;
if (title.isEmpty()) {
titleLocal = context.getResources().getString(R.string.reward_list);
} else {
titleLocal = title;
}
WebviewActivity.startActivity(context, url, "&zbuserid=" + id, titleLocal);
}
@Override
public void startFollwDetailActivity(Context context, int starId) {
Intent intent = new Intent(context, FollowDetailActivity.class);
intent.putExtra("buidx", starId);
context.startActivity(intent);
}
@Override
public void startLoginActivityActivity(Context context) {
context.startActivity(new Intent(context, LoginActivity.class));
}
@Override
public void startWebviewActivity(Context context, String url, String title) {
WebviewActivity.startActivity(context,
url,
title);
}
@Override
public void startVideoActivity(Context context, int vid) {
Intent intent = new Intent(context, VideoActivity.class);
intent.putExtra("vid", vid);
context.startActivity(intent);
}
@Override
public void startChallengeDetailActivity(Context context, int cid, String challengeName) {
Intent intent = new Intent(context, ChallengeDetailActivity.class);
intent.putExtra("cid", String.valueOf(cid));
intent.putExtra("challengeName", "");
context.startActivity(intent);
}
}
4)在Application中进行初试化
//跳转
ActivityManagerJumpImp.getIntance().init(new ActivityManagerJumpActivity());
5)在lib模块中进行调用
ActivityManagerJumpImp.getIntance()
.startRewardAnchorActivity(this, getResources().getString(R.string.reward_list), Contact.REWARD_ANCHOR_LIST, starId);
·······完······