8

I have 2 Activities, each in seperate applications. Activity1 has a button the user can click and it calls the second activity using an intent in its onClick() method:

Intent myIntent = getPackageManager().getLaunchIntentForPackage(com.myProject.Activity2);
startActivityForResult(myIntent, 600);

This correctly launches Activity2 from Activity1, but onActivityResult gets called in Activity1 before onCreate gets called in Activity2, instead of in onBackPressed() where I set up the return intent.

Here is the onCreate method for Activity2:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Here is the current version of onBackPressed method for Activity2:

@Override
public void onBackPressed() {
    Intent intent = new Intent();
    intent.putExtra("Stuff", someStuff);

    if(getParent()==null){
        setResult(Activity.RESULT_OK, intent);
    }else{
        getParent().setResult(Activity.RESULT_OK, intent);
    }
    finish();
    super.onBackPressed();
}

My AndroidManifest.xml has the following intent filter for Activity2:

<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

I verified that my launchMode is standard (and not singleTask, etc) as advised here and my request code is not negative as warned here. I also tried android:launchMode="singleTop", but that was a no-go also.

I also tried not calling finish() in onBackPressed() for Activity2 as mentioned here (also with just super.onBackPressed() as suggested here) and again calling it as suggested here.

Additionally I tried commenting out the line intent.putExtra("Stuff", someStuff); as it seemed to cause trouble for this person.

Any ideas as to what I might be doing wrong?

Community
  • 1
  • 1
user1205577
  • 2,388
  • 9
  • 35
  • 47

3 Answers3

10

So here is the final solution that took care of it:

I changed the intent for Activity1 to the following:

Intent myIntent = new Intent();
myIntent.setClassName("com.myProject", "com.myProject.Activity2");
startActivityForResult(myIntent, 600);

For some reason Android requires the fully qualified name for the second parameter in addition to the package name given by the first parameter. Now it works! :)

user1205577
  • 2,388
  • 9
  • 35
  • 47
  • 1
    Thank you very much for coming back and posting your solution. I have been stuck on this problem for a long time and I too tried all of the solutions you came across in your question to no avail. This fixed it and made my day a heck of a lot better =) – Kristen D. Oct 10 '12 at 20:02
  • I use startActivityForResult WITHOUT setClassName or fully qualified names all over my projects and they all work perfectly except for one case. So I tried your solution in that case and it didn't make any difference. – user316117 Mar 28 '13 at 19:10
  • 2h!! Thank you so mutch for the solution – Anthone Sep 10 '14 at 16:55
  • Sadly this haven't solved my issue, neither did the other answers! – Muhammad Alfaifi Dec 17 '14 at 10:36
  • 1
    This solved it for me too, in the specific case of communicating between two separate applications. – syonip Dec 17 '15 at 10:26
1

It will happen if "singleInstance" flag is set when you launch the activity.

jbaylina
  • 4,408
  • 1
  • 30
  • 39
0

Not certain what your problem is. The way you're creating the Intent in Activity1 is odd; that method isn't meant for creating intents that launch another activity in the same app. Some developers use the Intent(Context, Class<>) constructor. I prefer to use Intent(String action) with a custom action string defined only in my app (which is easier to code correctly).

Also, the intent filter you've specified for Activity2 is usually used for an activity that's launched directly from the Home screen.

Where's the onCreate() code for activity2? Where's the code for onBackPressed()? Can you prove to me that setResult() is called before some other code in Activity2? You should run the activities in debug. Ensure that Activity2 is receiving the intent you think it should, then trace step by step the statements that are executed until setResult(). The thing not to do is throw solutions at the code before you understand what the underlying problem is.

As far as I can tell so far, Activity1 is sending out an Intent, and then onActivityResult is being called. Nothing else is proven so far.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • Thanks for the comment - I've added the `onCreate` and `onBackPressed` methods to the question. I ran it debug many times and using break points I was able to show that Activity1's `onActivityResult` gets called when Activity2 first gets launched - before Activity2 ever returns, and does not get called again when it returns. – user1205577 Apr 09 '12 at 13:59
  • I also tried creating an intent using `Intent(context, Activity2.class)`, but it complained that it couldn't find the class (probably because it's in another application). I tried a few variations of using the fully qualified path name and that was a no-go also. – user1205577 Apr 09 '12 at 14:02