I want to start a new Intent dynamically. Therefore setClassName
seems the best choice.
First, I define 3 activity in Manifest
<activity android:name="com.example.pkg2.Act" />
<activity android:name="com.example.pkg1.Act1" />
<activity android:name="com.example.pkg1.Act2" />
From com.example.pkg2.Act
:
Intent intent = new Intent();
if(index == 0) intent.setClassName(Act.this, "com.example.pkg1.Act1");
else intent.setClassName(Act.this, "com.example.pkg1.Act2");
startActivity(intent);
And will get this exception:
Unable to find explicit activity class {com.example.pkg2.Act/com.example.pkg1.Act1}; have you declared this activity in your AndroidManifest.xml?
It looks like we can only use setClassName
to dynamically start new Activity but within the same package.
Any idea to solve this issue? All help is appreciated.
Use this code and you'll be fine.
You can use the following method to create the intent in the package context:
This way you keep on generic code.
HTH
The first param is the applicationId located in the build.gradle file
The second param is full path to of the class with its package. for example: intentObj.setClassName("applicatioId", "com.youCompany.yourAppName.YourClassName")
You can also launch Activities in this manner. Try this
intent.setClassName(packageName, className);
where
packageName - The name of the package implementing the desired component, i.e. the package where the caller belongs to.
className - fully qualified name of the class [from different package]
Calling from
com.example.pkg2.Act
:Follow the syntax to write setClassName() method: