I have two Android applications,suppose they are "A" and "B", "A" has five activities and I want to call its specific activity from button click event of "B". I tested this way of calling one application from another:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");
startActivity(LaunchIntent);
"com.testapp.ws" is the package name of "A".
This runs "A" from its first activity again not from the specific activity. How can I call A's specified activity?
Intent LaunchIntent = getPackageManager(). getLaunchIntentForPackage("com.testapp.ws"); startActivity(LaunchIntent); "com.testapp.ws" is the package name of "A". This runs "A" from its first activity again not from the specific activity.
Android inter-process communication At the simplest level, there are two different ways for apps to interact on Android: via intents, passing data from one application to another; and through services, where one application provides functionality for others to use.
Using intents even allows your app to start an activity that is contained in a separate app. An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").
Grant,
The issue here is clearly a misunderstanding of the Android Application Model. Commonsware is absolutely correct about how to solve this problem. However, without understanding Android fundamentals, I can see why you are having difficulty applying it. So, a quick explanation:
Every action in Android begins with an Intent. This is particularly true for Activities. Every Activity has an Intent. To make the interface easy for the developers, you may respond to an Intent from the OS, OR you may create an Intent from the Activities class to use. In general, it is best practice to do the first option.
Responding to an Intent
When picking an Intent to respond to, you may literally respond to any Intent. This is called an Action. If I created an Intent called "FOO", the Bar Activity could pick it up and respond. We have conventions, however, and the primary of those is to prepend your package name to any Intent you make. For example "com.company.package.FOO". Simply put, this is so that we avoid collisions with other apps.
Every Activity may respond to different events. This is defined in the AndroidManifest.xml.
<activity android:name="Activity3" ... >
<intent-filter>
<action android:name="com.company.package.FOO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Above, we also set the category to DEFAULT, so that unless the user changes it, we'll be the only app that responds to our custom Intent. The way that we then call the Intent is by using the SAME NAME that we created (i.e. "com.company.package.FOO")
startActivity(new Intent("com.company.package.FOO"));
That's how it works! You would simply change the above "com.company.package.FOO" to your package name (defined by your application) and something meaningful. An example is "com.testapp.ws.SWAT_FLIES".
Why your code doesn't work
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");
The above code looks for a specific KIND of Intent action. Remember when you made the AndroidManifest and the first Activity you put:
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
Well... getLaunchIntentForPackage() only gets the Intent for that first Activity. That's WHY we make a custom Intent... First, because we don't really want it to be our 3rd Activity to be our start up... And second, becuase the OS will tell us only the startup Activity. We have to tell it with our OWN action (i.e. "com.testapp.ws.SWAT_FLIES")
Hope this helps,
FuzzicalLogic
Step #1: Add an <intent-filter>
to the third activity with a custom action:
<intent-filter>
<action android:name="com.testapp.ws.SOMETHING_USEFUL"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Step #2: Start that activity using an appropriate Intent
:
startActivity(new Intent("com.testapp.ws.SOMETHING_USEFUL"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With