重現場景
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寫博客,特此記錄一下,感覺能很好的規範博客文章格式,看起來會舒服很多,以後寫博客都採用他來記錄了.