Issue
I am having an issue opening one of my other application using Intents in Android. Here is what I have right now:
val packageName = "com.myapp.demo.application"
val activityName = "$packageName.WelcomeActivity"
try {
val intent = Intent().apply {
setClassName(packageName, activityName)
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
}
startActivity(intent)
requireActivity().finish()
} catch (e: Exception) {
Toast.makeText(
context,
"App not installed",
Toast.LENGTH_LONG
).show()
}
}
And the manifest in the second application looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp.demo.application">
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
<activity android:name=".SimpleScannerActivity" />
<activity android:name=".MainActivity" />
<activity
android:name=".WelcomeActivity"
android:exported="true"
android:label="@string/title_activity_welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- Permissions and features declarations go here -->
<!-- ... -->
<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
</manifest>
I am always getting this exception:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapp.demo.application/com.myapp.demo.application.WelcomeActivity}; have you declared this activity in your AndroidManifest.xml?
What I am missing here?
I have tried starting another application with different package name and it worked with the same code, but not for my com.myapp.demo.application
. Maybe something is not right with my demo application manifest?
Solution
If you want to launch the start(activity with the catergory LAUNCHER ) activity of your appA from appB, then it can be done using :
val launchIntent = getPackageManager().getLaunchIntentForPackage("com.myapp.demo");
if (launchIntent != null) {
startActivity(launchIntent);
And if you want to open some other activity then create a deep link uri for that activity.
Add the data tag in manifest.xml of appA
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "demo://myapp” -->
<data android:scheme="demo"
android:host="myapp" />
</intent-filter>
Adding the queries tag in the manifest of appB
<queries>
<package android:name="com.myapp.demo"/>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
</queries>
Launching the activity of appA from appB
val uri = Uri.parse("demo://myapp")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.resolveActivity(packageManager)?.also {
startActivity(intent)
}
Answered By - zaid khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.