-1

I'm trying to figure out how to go to different activity depending on some parameter.

Please help me... The code below is my code though it doesn't work.

                            String className;
                            if (Parameter == 0) {
                                className = "class.A." + Parameter + "Activity.class";
                            } else if (Parameter == 1) {
                                className = "class.B." + Parameter + "Activity.class";
                            }
                            //activityClass = (Cast className to Class);

                            Intent intent = new Intent(ExampleActivity.this, activityClass);
                            intent.setAction(Intent.ACTION_VIEW);
                            startActivity(intent);

3 Answers 3

0

Try to code something like this. hope it will help you.

 Intent intent=null;
 if (Parameter == 0) {
          intent=new Intent(ExampleActivity.this,yourclass);
 } else if (Parameter == 1) {
          intent=new Intent(ExampleActivity.this,yourclass);
 }
 intent.setAction(Intent.ACTION_VIEW);
 startActivity(intent);
4
  • Thanks. But I want to change the class name depending on the parameter... like how I wrote
    – inductor
    Aug 25, 2017 at 12:48
  • you can define your class in if condition.like if (Parameter == 0) { intent=new Intent(ExampleActivity.this,ClassA.class); }else if (Parameter == 1) { intent=new Intent(ExampleActivity.this,CalssB.class); }
    – Akash
    Aug 25, 2017 at 12:49
  • It has "Parameter" itself in the class name in the string variable
    – inductor
    Aug 25, 2017 at 12:51
  • actually i didn't get you. explain in brief what you want to do?
    – Akash
    Aug 25, 2017 at 12:53
0

Try

Class className;
if (Parameter == 0) {
           className = Activity1.class;
} else  {
           className = Activity2.class;
}


Intent intent = new Intent(ExampleActivity.this, className );
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
1
  • Thanks. But I want to change the class name depending on the parameter... like how I wrote
    – inductor
    Aug 25, 2017 at 12:47
0

I'm not sure I got you, but you can try to use Class.forName() function. Your code would look like this:

String className;
if (Parameter == 0) {  
    className = "class.A." + Parameter + "Activity";
} else if (Parameter == 1) {
    className = "class.B." + Parameter + "Activity";
}

Class activityClass = Class.forName(className);

Intent intent = new Intent(ExampleActivity.this, activityClass);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);

Remember that you have to skip ".class" extenstion in your className String.

1
  • This is what I wanted! :D Thank you!
    – inductor
    Aug 28, 2017 at 6:24

Your Answer

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

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