alibaba / ARouter

💪 A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)
Apache License 2.0
14.45k stars 2.61k forks source link

Kotlin使用,在页面传递List<Any>列表集合 Serializable对象时取值界面使用@Autowired获取不到 #961

Open pangli opened 3 years ago

pangli commented 3 years ago

//传递方式使用Bundle传递数据 //List值说明

val data = ArrayLis<CartProduct>()

//CartProducty对象实现Serializable接口

@JsonClass(generateAdapter = true)
data class CartProduct(
    val currencySymbol: String,
    @Json(name = "count")
    var count: Int = 0,
    @Json(name = "code")
    val productId: String,
    @Json(name = "img")
    val productImg: String,
    @Json(name = "name")
    val productName: String,
    @Json(name = "price")
    val productPrice: Double,
    var checked: Boolean = false
) : Serializable

//构建Bundle

val bundle=bundleOf(“data_list” to data )
ARouter.getInstance().build(path).with(bundle) .navigation(context)

//获取数据

@Autowired(name = “data_list”)
@JvmField
var products: List<CartProduct>? = null

//此时获取到的products为null

//ARouter生成代码

public class OrderConfirmActivity$$ARouter$$Autowired implements ISyringe {
  private SerializationService serializationService;

  @Override
  public void inject(Object target) {
    serializationService = ARouter.getInstance().navigation(SerializationService.class);
    OrderConfirmActivity substitute = (OrderConfirmActivity)target;
    substitute.isEdit = substitute.getIntent().getBooleanExtra("isEdit", substitute.isEdit);
    if (null != serializationService) {
      substitute.products = serializationService.parseObject(substitute.getIntent().getStringExtra("Products"), new com.alibaba.android.arouter.facade.model.TypeWrapper<List<CartProduct>>(){}.getType());
    } else {
      Log.e("ARouter::", "You want automatic inject the field 'products' in class 'OrderConfirmActivity' , then you should implement 'SerializationService' to support object auto inject!");
    }
  }
}

debug发现serializationService 为null

不使用@Autowired(name = “data_list”)取值换为手动解析Intent

val products= intent.getSerializableExtra( “data_list”) as List<CartProduct>

此时值不为空数据获取成功

疑问为什么无法使用注解获取数据?我需要怎样处理才能使用注解?

注解生成的代码为什么不使用substitute.getIntent().getSerializableExtra( “data_list”)直接获取到对象而是判断serializationService是否为null使用SerializationService来处理数据

lysung commented 3 years ago

我使用Gson解析,这样做可以。

@Route(path = "/service/serialize/gson")
class GsonSerializationService: SerializationService {

    var gson: Gson = Gson()

    override fun init(context: Context?) {

    }

    override fun <T : Any?> json2Object(input: String?, clazz: Class<T>?): T {
        return gson.fromJson(input, clazz)
    }

    override fun object2Json(instance: Any?): String {
        return gson.toJson(instance)
    }

    override fun <T : Any?> parseObject(input: String?, clazz: Type?): T? {
        return gson.fromJson(input, clazz)
    }
}