9

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

4
  • Out of curiosity , why are you using generic activity ?
    – Mr.Me
    Mar 21, 2013 at 18:06
  • This doesn't have anything to do with the Intent constructor; the problem is that your syntax is wrong. NavegadorActivity<Clientes>.class is not valid syntax (since you can't combine generics with .class), and neither is NavClientes.class (since NavClientes is a variable-name, not a type-name).
    – ruakh
    Mar 21, 2013 at 18:10
  • (There's also a conceptual problem with what you're trying to do. If stringList is an ArrayList<String> and intList is an ArrayList<Integer>, then stringList.getClass() and intList.getClass() will be the same instance of Class. You simply cannot use Class to pass in generic type information, because that information is erased at runtime.)
    – ruakh
    Mar 21, 2013 at 18:12
  • @Mr.Me I'm trying to do a datagrid in NavegadorActivity that show objects of T
    – Heberfa
    Mar 21, 2013 at 18:18

2 Answers 2

4

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");
1
  • With this I get an other problem, in NavegadorActivity I need to declare another generic object DbAdapter<T> but myGeneric is a class and I can't convert it in a Type again.
    – Heberfa
    Mar 21, 2013 at 19:12
1

what's wrong with

Intent intent = new Intent(v.getContext(), NavegadorActivity.class);
3
  • How the constructor of NavegadorActivity will deal with T class?
    – Heberfa
    Mar 21, 2013 at 18:08
  • It is unnecessary to show the constructor since it is a generic class and need to know what is the class T
    – Heberfa
    Mar 21, 2013 at 18:14
  • @Heberfa: you asked how to pass the Class object representing NavegadorActivity to the Intent constructor. There is just one NavegadorActivity class object at runtime, which can be accessed using NavegadorActivity.class. This answer answers your question. Constructor of NavegadorActivity has nothing to do with the question
    – newacct
    Mar 22, 2013 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.