Intent and start activity from string

后端 未结 5 1313
Happy的楠姐
Happy的楠姐 2021-01-05 15:36

I have a little problem. I want to start activity but in something other way. I know that

Intent i = new Intent(this, ActivityTwo.class); 

相关标签:
5条回答
  • 2021-01-05 15:48

    Just use....

      Intent intent = new Intent().setClassName(activity,"packageName"+"className");
      startActivity(intent);
    
    0 讨论(0)
  • 2021-01-05 15:52


    Class<?> c =Class.forName("YOUR STRING" );
    Intent intent = new Intent(FirstActivity.this, c);
    startActivity(intent);
    
    0 讨论(0)
  • 2021-01-05 15:58

    Try this: startActivity(this, Class.forName(yourStringClass));

    0 讨论(0)
  • 2021-01-05 16:00

    You can look up a Class by name using Class.forName("MyString")

    0 讨论(0)
  • 2021-01-05 16:10

    Here is a code by which you can start activity using the name of the activity

    Class<?> c = null;
    if(StringClassname != null) {
        try {
            c = Class.forName(StringClassname );
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Intent intent = new Intent(mail.this, c);
    startActivity(intent);
    

    Here class name will be full name of the class with the package name. For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.

    0 讨论(0)
提交回复
热议问题