重现场景
A页面启动B页面,B页面在AndroidManefest.xml中设置启动模式为SingleTop,此时在A页面中通过Arouter重复启动B页面,此时会启动多个B页面,而不仅仅只启动一个B页面;
如果不使用Arouter,而使用startActivity这种方式,则只会启动一次B页面,而不会启动多个B页面.
解决方式
此时的Activity启动启动模式需要放在路由跳转中去才回生效,代码如下:
val navigation = object : NavigationCallback {
override fun onFound(postcard: Postcard) {}
override fun onLost(postcard: Postcard) {}
override fun onArrival(postcard: Postcard) {}
override fun onInterrupt(postcard: Postcard) {}
}
ARouter.getInstance().build("/example/BActivity").withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation(context, navigation)
ARouter.getInstance().build("/example/BActivity").withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation(context, navigation)
ARouter.getInstance().build("/example/BActivity").withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation(context, navigation)
ARouter.getInstance().build("/example/BActivity").withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation(context, navigation)
注意用的是kotlin代码,那个or在java中用“|”表示,末尾加个符号“;”,需要用.withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)替代启动模式,才会生效.
如果是singleTast这种启动模式,在AndroidManifest.xml文件中配置即可生效,无需使用withFlags这种方式来定义启动模式,这点还是感觉比较奇怪的.
第一次用markdown写博客,特此记录一下,感觉能很好的规范博客文章格式,看起来会舒服很多,以后写博客都采用他来记录了.