Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onActivityResult called early

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?

like image 247
user1205577 Avatar asked Apr 06 '12 20:04

user1205577


People also ask

What is the replacement for startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

What is use instead of startActivityForResult in Android?

In place of startActivityForResult(intent, 123), use Intent intent = new Intent(this, SampleActivity.


1 Answers

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! :)

like image 140
user1205577 Avatar answered Sep 29 '22 08:09

user1205577