Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i pass a generic class as param to Intent constructor

I have this generic activity in my android application

public class NavegadorActivity<T> extends Activity {

    ....
...
}

And I'm trying to call it as below

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

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);

Which doesn't work either.

Nor this

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?

Thanks

like image 363
Heberfa Avatar asked Mar 21 '13 18:03

Heberfa


1 Answers

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());

and retreive that class object via

Class<?> myGeneric = Class.forName(intent.getStringExtra("class", "java.Object");
like image 137
Mr.Me Avatar answered Sep 18 '22 13:09

Mr.Me