Friday, August 26, 2011

Android : Calling the other package activity from the current package Activity

Hi All,
After a long time one more android blog, or you can say a new problem which I encounter. In general cases we call activity using intent within the same package. but what if we want to call some other package activity. So here is the solution. Here I will be using the name convention as Calling Activity as First Activity and Called Activity as Final Activity.
Method 1 : Using setClass() method in Intent class.
It is the same thing which you do in normal scenario but with a little tweak in it. In FirstActivity where ever you want to call other activity from other package, put following code
Intent i = new Intent();
i.setClassName("com.android.finalactivity", "com.android.finalactivity.FinalActivity");
startActivity(i);
/* Here com.android.finalactivity is the package name and com.android.finalactivity.FinalActivity is full class name. */
Now go to the AndroidManifest.xml of the FirstActivity and Add following line
activity android:name=”com.samsung.finalactivity.FinalActivity” in application tag.
It will work fine.
Method 2. Using action name
For this first go to your Final Activity and Modify the Manifest file like this :
Instead of the action android :name =”android.intent.action.MAIN” Use
action android:name=”finalApplication.intent.LAUNCH”
Now go to your FirstActivity.java and write
Intent i = new Intent("finalApplication.intent.LAUNCH");
startActivity(i);
Here no need to change AndroidManifest file for FirstApplication.
Hope it helps.

1 comment:

suyog said...

made it very simple...thanks a lot!!!