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
- startActivityForResult()
- onActivityResult()
- onCreate() (new activity)
- 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
How to Import Existing Android Project into Eclipse
How to Decompile an Android .Apk File
How to Play Ringtone/Alarm Sound in Android
How to Change the Color of Alertdialog Title and the Color of the Line Under It
How to Pass the Values from One Activity to Previous Activity
Counting Chars in Edittext Changed Listener
How to Keep the Screen on in My App
How to Hide Soft Keyboard When Activity Starts
How to Switch Activity Without Animation in Android
How to Implement a Long Click Listener on a Listview
Couldn't Get Connection Factory Client
Usage of Forcelayout(), Requestlayout() and Invalidate()
Gridview with Two Columns and Auto Resized Images
Where Is the Layout Preview in Android Studio
Android Facebook Sdk 4 in Eclipse