0

I have been trying to open an Activity from a different app. But is doesn't work. Toast message appears Absent as u can see from code. The Activity I want to open is from package com.example.samarth.upbuttondemo.ActivityA. This Activity has an intent filter. The XML file for the activity I want to open is

 android:parentActivityName="com.example.samarth.upbuttondemo.MainActivity">
        <intent-filter>
            <action 
  android:name="com.example.samarth.upbuttondemo.ActivityA" />

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



 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 import java.util.List;

 public class MainActivity extends AppCompatActivity {
 Button b1;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1=(Button)findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i.setComponent(new ComponentName("com.example.samarth", 
    "com.example.samarth.ActivityA"));
            PackageManager packageManager = getPackageManager();
            List activities = packageManager.queryIntentActivities(i,
                    PackageManager.MATCH_DEFAULT_ONLY);
            boolean isIntentSafe = activities.size() > 0;
            if(isIntentSafe==true)
            {


      Toast.makeText(MainActivity.this,"Present",Toast.LENGTH_SHORT).show();
                startActivity(i);
            }
            else
            {

       Toast.makeText(MainActivity.this,"Absent",Toast.LENGTH_SHORT).show();
            }

        }
    });
}
}
  • Check this post https://stackoverflow.com/questions/2209513/how-to-start-activity-in-another-application – akari Jun 01 '17 at 16:52
  • Possible duplicate of [How to start activity in another application?](https://stackoverflow.com/questions/2209513/how-to-start-activity-in-another-application) – Drise Jun 01 '17 at 16:57

2 Answers2

1

Write this to open whatsapp for example:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(launchIntent);

You only have to pass the package of your app as parameter of the method getLaunchIntentForPackage("com.yourpackage")

Curio
  • 1,331
  • 2
  • 14
  • 34
0

Lets say you want to navigate from activiry x from App A to activity y from App B.

so add this in activity x (App A):

//calling an activity using <intent-filter> action name 
Intent inent = new Intent("com.hmkcode.android.another.app.ANOTHER_ACTIVITY");

startActivity(inent);

Now add this intent filter in the manifest of activity y (App B)

<intent-filter>
      <action android:name="com.hmkcode.android.another.app.ANOTHER_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

More details and explanation here in this link

Salim Lachdhaf
  • 1,073
  • 1
  • 9
  • 21