Android onActivityResult called early

I have 2 classes, each in separate applications. Activity1 has a button that the user can click, and it invokes the second action using the intent in its onClick() method:

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

This starts Activity2 correctly from Activity1, but onActivityResult is called in Activity1 before onCreate is called in Activity2, not in onBackPressed() , where I set 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 the 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 confirmed that my launchMode standard (not singleTask , etc.) is as recommended here , and my request code is not negative, as warned here . I also tried android:launchMode="singleTop" , but that wasn't there either.

I also tried not to call finish() in onBackPressed() for Activity2, as mentioned here (also using only super.onBackPressed() , as suggested here ), and again called it suggested here .

In addition, I tried to comment out the line intent.putExtra("Stuff", someStuff); as it seemed to be causing problems for this person .

Any ideas on what I might be doing wrong?

+8
android android-intent android-activity
source share
3 answers

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

I changed the intent of Activity1 to the following:

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

For some reason, Android requires the full name of the second parameter in addition to the package name specified by the first parameter. Now it works! :)

+9
source share

This will happen if the "singleInstance" flag is set when the operation starts.

+1
source share

I don’t know what the problem is. The way you create an intent in Activity1 is odd; this method is not intended to create intentions that trigger another action in one application. Some developers use the Intent constructor (Context, Class <>). I prefer to use Intent (String action) with a custom action string defined only in my application (which is easier to code correctly).

In addition, the intent filter that you specified for Activity2 is usually used for activity that runs directly from the main screen.

Where is the onCreate () code for Activity2? Where is the code for onBackPressed ()? Can you prove to me that setResult () is called before other code in Activity2? You must run actions in debugging. Make sure Activity2 gets the intent you think should, then follow the step-by-step instructions that run before setResult (). It is not a matter of throwing decisions in the code before you understand what the main problem is.

As far as I can tell, Activity1 sends an Intent and then onActivityResult is called. Nothing has been proven yet.

0
source share

All Articles