converting a string to a class

后端 未结 2 1720
傲寒
傲寒 2021-01-03 07:27

I\'m trying to move between one Activity to another based upon some user input.

I\'m trying to use:

String myClass = \"some_user_input.class\"
Intent         


        
2条回答
  •  一整个雨季
    2021-01-03 07:41

    The reflection mechanism allows you to do it:

    String myClass = "some_user_input";
    Class clazz = Class.forName(myClass);
    Intent myIntent = new Intent(getApplicationContext(), clazz);
    

    Note that those classes should be included in the android manifest XML.

    Also note that I didn't handle the exception in this example :)

提交回复
热议问题