0

I try to launch another app in my app

There is 2 methods I have tried.

Method 1

    Intent intent = new Intent(Intent.ACTION_MAIN);
    ComponentName componentName = new ComponentName("com.b_app", "com.b_app.MainActivity");
    intent.setComponent(componentName);
    startActivity(intent);

Method 2

        Intent intent =  getPackageManager().getLaunchIntentForPackage("com.b_app");
        if (intent != null) {
            startActivity(intent);//null pointer check in case package name was not found
        }
        else Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();

Method 1 gives me error :

Unable to find explicit activity class {com.b_app/com.b_app.MainActivity}; have you declared this activity in your AndroidManifest.xml?

Method 2 the Toast shows Error (my else condition),meaning getPackageManager() returns null..

My Manifest in b_app

   <activity android:name="com.b_app.MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and I'm pretty sure the b_app is installed in my smartphone..

How can I solve it?

achicn3
  • 93
  • 2
  • 9

2 Answers2

3

the Toast shows Error (my else condition),meaning getPackageManager() returns null

No. You would be getting a NullPointerException in that case. In your case, getLaunchIntentForPackage() is returning null.

That, plus your other symptoms, suggests that you do not have an application whose applicationId is com.b_app installed on this device.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

There is a little chance to meet in my sitution.. One of my app was returned null for "getLaunchIntentForPackage" and I was recognize My app is not shown in Device Although it is installed.. Recently I implement Firebase Dynamic... And my application manifest configured as wrongly

Here is wrong one :

<intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

 </intent-filter>

Correct one is

  <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

            </intent-filter>

If you implement wrongly your application manifest, User cant install your app... because Google Play "Open" button app is geting invisible

Ucdemir
  • 2,852
  • 2
  • 26
  • 44