0

activity A starts activity B (in another app) with

Intent i;
PackageManager manager = getPackageManager();
try {
    i = manager.getLaunchIntentForPackage("my.other.app.package.name");
    if (i == null)
        throw new PackageManager.NameNotFoundException();
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivityForResult(i, 1);

} catch (PackageManager.NameNotFoundException e) {

}

this is my activity B:

setContentView(R.layout.main_layout);
Button mybutton = (Button) findViewById(R.id.button1);
mybutton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        ArrayList<String> data = new ArrayList<String>();
        data.add("1.2");
        Intent returnIntent = new Intent();
        returnIntent.putExtra("data", data);
        setResult(RESULT_OK, returnIntent);
        finish();
    }
});

My problem is that onActivityResult() in activity A is called immediately (resultCode=0) when activity B is started, instead of waiting for me to press the button. I have tried setting the launchmode of both activities to singleTop or standard (as suggested here) but that did not solve it:

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="standard">

What am I doing wrong?

Community
  • 1
  • 1
Niels
  • 1,340
  • 2
  • 15
  • 32
  • 1
    Have a look at this link http://stackoverflow.com/questions/10048813/android-onactivityresult-called-early?rq=1 Hope it solves your problem – Periklis Douvitsas Apr 08 '14 at 09:17
  • Do you see "Activity is launching as a new task, so cancelling activity result." in your logcat? – marcinj Apr 08 '14 at 09:19
  • Awesome, thanks @Periklis! That solved it. I've been searching all over stackoverflow but didn't find that one. – Niels Apr 08 '14 at 09:23

0 Answers0