Onactivityresult() Called Prematurely

onActivityResult() called prematurely

This is fixed by changing the launch mode to singleTop:

    <activity
android:name=".MainActivity"
android:launchMode="singleTop">

There's a bug / feature (?) in Android, which immediately reports result (which has not been set yet) for Activity, declared as singleTask (despite the fact that the activity continues to run). If we change launchMode of the parent activity from singleTask to singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTask activity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.

onActivityResult called too early

I would rather rely on event based code logic rather than on probability. You could try the following.

*1) The user is in the DetailActivity and changes some data setting the dirty flag to true.
2) The user clicks the system back button.
*

Override the onBackPressed method and perform your database operation right there and once the DB operation is performed you can call the super.onBackPressed() method and let the Android Activity stack take over.
Mind you DB operation could be time consuming and hence it is also recommended to use AsyncTask with callback and only then call finish().

Android onActivityResult called early

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

OnActivityResult called prematurely with Settings menu

This is how I solved this issue.

In reference to the answer posted for a similar question, I changed startActivityForResult() to startActivity() as the order of the action calls is like

  1. startActivityForResult()
  2. onActivityResult()
  3. onCreate() (new activity)
  4. setResult() or finish()

The control is taken to the settings page where the user can switch on/off the Wi-Fi and later come back to the app. :)

onActivityResult gets called when Activity starts, not when its finished

It is a bug of Android

onActivityResult() called prematurely

and

Why does result from startActivityForResult come before activity really starts?

I don't know which version you use and if it has been solved/corrected



Related Topics



Leave a reply



Submit