0

I have an array of Classes, like this:

private static final Class[] testerClasses={
    Tester1.class
};

I need it to be like this, so I can start Android intents, like this:

intent =new Intent(this,testerClasses[0]);
startActivity(intent);

I'm trying to figure out how to do the following, and I'm really struggling to figure out how to do it...

  1. I need to call functions from an instance of the class. For the most part, these are static or static like functions, so merely getting an instance of the function is sufficient.
  2. I need to make sure I can pass the class as the Intent structure requires.

Why do I want to do this? Sometimes I want to call the function like an intent, other times I just want to get data about the intent, without having to call it. My abstractTester class (Of which all concrete tester classes inherit from) contains a few things like the name of the test, which is handy to have before the Intent has even started.

Thanks for the help!

3 Answers 3

1

You will not be able to pass the class object with the Intent . Instead, pass the class name in the intent; the receiver can get the class via loadClass()

Once the receiver has the class object, it can use reflection to call the intended method.

Sign up to request clarification or add additional context in comments.

Comments

1

The correct solution is:

private static final abstractTester[] testerClasses={
    new Tester1()
};

intent =new Intent(this,testerClasses[0].getClass());
startActivity(intent);

Then if I need to reference the class, I can just do it. Works like a charm!

Comments

0

Hi you can try to use code below ,

ArrayList<Class<?>> a = new ArrayList<Class<?>>();
a.add(MainActivity2.class);
a.add(MainActivity.class);

Intent intent =new Intent(this,a.get(0));
startActivity(intent);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.