Skip to content Skip to sidebar Skip to footer

Start A New Intent By SetClassName With Same Package In Android

I have to start a activity class , i know how to start an activity but the problem is that my class name is dynamic. I mean we have 5 activity named 1. Events 2. Notification 3. C

Solution 1:

I think you are just missing a . character in your specified string for the Intent in the following line:

intent = new Intent().setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification" + NameOfClass);

Change it to:

intent = new Intent().setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass);

EDIT:

If that didn't work, how about trying the following:

Class<?> clazz = Class.forName("com.mcm.menuandnotification." + NameOfClass); 
intent.setClass(this, clazz);

Solution 2:

Have a look at this answer.

Intent intent  = new Intent(Intent.ACTION_MAIN).addCategory(
intent.CATEGORY_LAUNCHER).setClassName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("com.mcm.menuandnotification",
        "com.mcm.menuandnotification." + NameOfClass));

Cannot start new Intent by setClassName with different package in Android


Post a Comment for "Start A New Intent By SetClassName With Same Package In Android"