Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() called prematurely

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?

like image 798
Eugene Mayevski 'Callback Avatar asked Jul 28 '10 16:07

Eugene Mayevski 'Callback


People also ask

What is called after onActivityResult?

you are right, onActivityResult() is getting called before onResume().

What is the use of onActivityResult in Android?

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.

How can we call fragment onActivityResult from 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.

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.


1 Answers

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.

like image 175
Eugene Mayevski 'Callback Avatar answered Sep 25 '22 10:09

Eugene Mayevski 'Callback