ARouter的坑

navigation()是使用application的context去发起跳转的,有些场景要注意特别是使用Intent.FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_NEW_TASK要慎用,要结合上下环境,因为等于是使用了新 task,导致很多变量的值都消失了。

在ARouter里还是使用navigation(this)靠谱, 并且withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP)
withFlags 只用一次

1
2
3
ARouter.getInstance().build(ARouterPathType.MainActivity)
.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.navigation(this);

当出现
.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.navigation();

会报错

1
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.navigation(this);
正常,Intent.FLAG_ACTIVITY_CLEAR_TOP生效 建议使用这种

.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.navigation();
正常,Intent.FLAG_ACTIVITY_CLEAR_TOP生效

1
2
3
@Autowired(name = OpenSourceARouter.DataSetService)
@JvmField
internal var dataSetService: DataSetService? = null

kotlin使用注解要用@jvmField不然会报private不能注解的错误