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>