0

I have two separate Android apps, ApplicationA and ApplicationB. I wish to open ApplicationA, type some data into an EditText, and send that value to ApplicationB. In ApplicationB, I wish to perform some actions and then send a value from an EditText in ApplicationB back to ApplicationA. Currently, I am doing this by using Intents:

    ApplicationA     ----->    ApplicationB        ----->        ApplicationB        ----->   ApplicationA

Start MainActivity in        Handle the Intent from           Perform some actions,       Handle the Intent from
ApplicaitonB, sending        ApplicationA, and read           then start MainActivity     ApplicationB, and read                                              
some data                    the Intent's data                in ApplicationA, sending    some data
                                                              some data

As you can see, I am using one intent to to go from A -> B, and another one to go from B -> A.

It would seem much easier to start ApplicationB's MainActivity using an Intent from ApplicaitonA and calling startActivityForResult(), while implementing onActivityResult() in ApplicationA to handle the respone from ApplicationB. The problem with this is that it seems like startActivityForResult() returns immediately, so there is no time for the user in ApplicationB to perform any actions before returning the data to ApplicationA. It seems like this would work if both Activities were in the same application, but since they are in different applications, startActivityForResult() is always returning immediately.

This is the MainActivity class of ApplicationA:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final EditText et = (EditText) this.findViewById(R.id.someDataInA);  

    final Button btn = (Button) this.findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = MainActivity.this.getPackageManager().getLaunchIntentForPackage("com.comp.ActivityB");
            MainActivity.this.startActivity(i);
        }
    });
}

And this is the MainActiivty class of ApplicationB:

In@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final EditText et = (EditText) this.findViewById(R.id.someDataInB);

    final Button btn = (Button) this.findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = MainActivity.this.getPackageManager().getLaunchIntentForPackage("com.comp.ActivityA");
            MainActivity.this.startActivity(i);
        }
    });
}

Here is the AndroidManifest for ApplicationA:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

and the AndroidManifest for ApplicationB:

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
user2121620
  • 678
  • 12
  • 28

2 Answers2

0

Your problem is probably that the applications are starting in new tasks or clearing the task stack. Since you are getting the launch intent and not using an IntentFilter or other structure to target and stack the activities, startActivityForResult is not useful.

You should look at something like this (although this probably more than you need):

Using startActivityForResult across Android applications

(You would need to post your manifests to give more insight into this problem.)

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

You can definitely use startActivityForResult() to start B from A. However, you don't want to use a "launch Intent" when doing this, because a "launch Intent" will start app B in a separate task. The mechanism that startActivityForResult() uses does not work across tasks.

Try this in app A instead:

    public void onClick(View v) {
        Intent i = new Intent();
        i.setClassName("com.comp.ActivityB", "com.comp.ActivityB.MainActivity");
        MainActivity.this.startActivityForResult(i);
    }

In app B, instead of calling startActivity() to go back to app A, you can just do this:

    public void onClick(View v) {
        Intent i = new Intent();
        // Put data into the Intent to return it to calling app
        i.putExtra("name1", value1);
        i.putExtra("name2", value2); // ...
        setResult(RESULT_OK, i);
        finish();
    }
David Wasser
  • 93,459
  • 16
  • 209
  • 274