我如何传递一个泛型类的参数,以构造意向意向、参数、泛型类

2023-09-07 17:24:10 作者:一个忧伤的美男子

我在我的Andr​​oid应用程序这个通用的活动

I have this generic activity in my android application

public class NavegadorActivity<T> extends Activity {

    ....
...
}

和我想它调用如下

Intent intent = new Intent(v.getContext(), NavegadorActivity<Clientes>.class);

然而,意图构造函数不接受一个泛型类的参数。然后,我想这code

However, Intent constructor doesn't accept a generic class as param. Then I tried this code

Class<NavegadorActivity<Clientes>> NavClientes =  NavegadorActivity.class;

Intent intent = new Intent(v.getContext(), NavClientes.class);

这也不行。

这也不

Class<NavegadorActivity<Clientes>> NavClientes =  NavegadorActivity<Clientes>.class;

Intent intent = new Intent(v.getContext(), NavClientes.class);

任何人都知道如何传递一个泛型类的参数,以意图构造?

Anyone know how can i pass a generic class as param to Intent constructor?

感谢

推荐答案

我不相信Android的意图系统支持通用活动,你不应该只是创建一个活动对象你的自我。我建议通过

I don't believe that android Intent System supports Generic activities and you shouldn't just create an activity object your self. I suggest to add an inner class to your activity that represents the generic functionality and pass that generic name via

intent.putExtraString("class", yourClass.getName());

并通过

Class<?> myGeneric = Class.forName(intent.getStringExtra("class", "java.Object");