How can I start a new android activity using class name in a string?

前端 未结 4 1461
情深已故
情深已故 2020-11-28 06:21

I\'m having a problem with an android application that I\'m working on.

My application has several sections and the next screen that loads is based on a string. So,

4条回答
  •  北海茫月
    2020-11-28 06:53

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

    String activityToStart = "com.example.MainActivity";
    try {
        Class c = Class.forName(activityToStart);
        Intent intent = new Intent(this, c);
        startActivity(intent);
    } catch (ClassNotFoundException ignored) {
    }
    

    EDIT

    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.

提交回复
热议问题