I start the Activity
(descendant of PreferenceActivity
) from my worker activity as follows:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1458) loadInfo(); } void showSettingsDialog() { startActivityForResult(new Intent().setClass(this, MyConfigure.class), 1458); }
MyConfigure
class does NOT have any setResult()
calls. In fact, MyConfigure
class doesn't have any code except OnCreate()
where it loads preferences using addPreferencesFromResource
.
Now onActivityResult
is called with requestCode
of 1458
prematurely, right after MyConfigure
activity is run. Tested on 1.6 and 2.1 emulators as well as 2.1 device. Is there a call to setResult()
buried somewhere in PreferenceActivity
? Or how else can this premature call be explained?
you are right, onActivityResult() is getting called before onResume().
When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.
To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.
But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With