[英]Sending argument to Fragment from Activity using NavigationComponent and SafeArgs with the same name reference
我在這里有一個案例,我必須從活動中打開一個片段,我有時會這樣做,但這里的問題是我想打開與之有爭議的片段。
<fragment
android:id="@+id/productDetailsFragment"
android:name="com.a.ry.ui.product.ProductDetailsFragment"
android:label="fragment_product_details2"
tools:layout="@layout/fragment_product_details" >
<argument
android:name="productId"
app:argType="integer" />
</fragment>
我所知道的是我可以打開一個片段(來自活動)並像這樣傳遞它的參數:
val bundle = Bundle()
bundle.putInt("productId", productIdValue)
navController.navigate(R.id.productDetailsFragment, bundle)
問題出在這里“productId”如果另一個開發人員從 navigation.xml 文件更改該名稱怎么辦,所以這根本不是最佳實踐
我想對該參數名稱進行相同的引用。
或者,如果有另一種解決方案?
您應該在 string.xml 中定義它並在 XML 和代碼中使用。
android:name="@string/VAR"
getString (R.string.VAR);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.