0

I have two own apps, App A and App B. An activity of App A, open App 2 by passing a String.

Code App1:

Intent launchIntent = getMainActivity().getPackageManager().getLaunchIntentForPackage("com.example.app2");
if (launchIntent != null)
{
  launchIntent.setAction(Intent.ACTION_SEND);
  launchIntent.putExtra("stringApp1ToApp2", "myString");
  launchIntent.setType("text/plain");
  startActivity(launchIntent);
}

Code App2:

Bundle parameters = this.getIntent().getExtras();
if(parameters != null)
{
  String b = parameters.getString("stringApp1ToApp2", "stringDefault");
}

Works correctly.

My problem is when I want to send a String from App2 to App1. In application 2 there is a button, that when you click you have to close the application (finish ()) and send a string to application 1. But do not open App1 from the beginning..

Any ideas?

Thank you in advance.

  • I will not provide you the whole solution, but what are you looking for is a BroadcastReceiver: https://developer.android.com/reference/android/content/BroadcastReceiver Basically: 1. you register in App1 Broadcast receiver with intentfilter. 2. In the App2 you create intent with string you want to send, you use context.sendBroadcast() method to send this broadcast to your App1. – Nimdokai May 20 '19 at 10:58

2 Answers2

0

i believe you need to override method OnNewIntent in your mainactivity in app 1, sence this is a new intent not a startintent

Joachim Haglund
  • 775
  • 5
  • 15
0

You can achieve this by using intent filter. Define Activity of app1 in manifest like following.

   <activity
        android:name=".App1Activity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:host="send_data"
                android:scheme="app1" />
        </intent-filter>
    </activity>

you can start App1Activity from app2 using

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("app1://send_data?string_App2_To_App1=myString"));
            try {
                startActivity(intent);
            } catch (e: Exception) {
                Util.showToast(this, "Activity not found");
            }

now you can get data sent by app2 to app1 by using code in Activity App1Activity's onCreate/onNewIntent()

    Intent intent = getIntent();
    Uri data = intent.getData();
    if (data != null) {
        String stringFromApp2 = data.getQueryParameter("string_App2_To_App1");
    }
TN Yadav
  • 99
  • 2
  • 5
  • But when I call the "startActivity" from Application 2, it starts Application 1 again. – Enric Sauret May 20 '19 at 09:00
  • Yes, it will start Application on you will land on "App1Activity". Which Activity do want to start from app2? – TN Yadav May 20 '19 at 09:16
  • It will not start Appliaction again if you set launch mode (singleTask/SingleTop as per your requirment)for App1Activity in manifest. If you define, you will get data on OnNewIntent of App1Activity. – TN Yadav May 20 '19 at 09:24
  • Sorry! Do not explain me clearly. My app1 consists of 2 activities: LoginActivity (initial) and MainActivity (composed of 7 fragments). I want to launch the other application (App2) from MainActivity.Fragment01 – Enric Sauret May 20 '19 at 09:35