Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult gets called when Activity starts, not when its finished

i wrote a FragmentActivity with some tabs. when i call an additional activity (which i use for setting user-preferences) with startActivityForResult (no differences if its in FragmentActivity or in ListFragment) the method onActivityResult gets called when i starts this preference-activity, but not when i finish it, as i would expect it (again no differences if its in FragmentActivity or in ListFragment). after i finish the preference-activity this method does not get called at all.

my problem is that i want to refresh my current tab (and set the last used tab id) after i finished the preference activity and i hoped to be able to do this in the onActivityResult method.

this is the class creating the preference-activity:

public abstract class ListFragmentBase<I> extends ListFragment implements
LoaderCallbacks<List<I>> {

this is the method forwarding me to the preference activity inside this class:

protected void forwardToPreferences(int currentTab){            
        Intent intent = new Intent(getActivity(), GlobalPreferencesActivity.class);
        getActivity().startActivityForResult(intent, 10);
}

this is the method that gets called after calling the method above but not after i finished the called activity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

this is the preference-activity:

public class GlobalPreferencesActivity extends Activity {

and inside that class you see how i call the finish method:

TextView confirmSettings = (TextView) view.findViewById(R.id.confirm_settings);
confirmSettings.setTextSize(PreferenceHelper.getSizeHeader(getApplicationContext()));
    confirmSettings.findViewById(R.id.confirm_settings).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {    
            if(BaseFragmentActivity.lastActivity != null){
                BaseFragmentActivity.lastActivity.onRefreshData();
        }
            ComponentName callingActivity = getCallingActivity();
            GlobalPreferencesActivity.this.finish();
        }
    });
like image 902
richard Avatar asked Aug 16 '13 10:08

richard


People also ask

When on activity result is called?

onActivityResult gets called when Activity starts, not when its finished.

Which method should you use to start activity and get the result back?

By the help of android startActivityForResult() method, we can get result from another activity. By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.

How do I pass onActivityResult from activity to fragment?

Since Activity gets the result of onActivityResult() , you will need to override the activity's onActivityResult() and call super. onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.

What is request code startActivityForResult?

The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.


2 Answers

Please make sure activity your calling is not singleInstance in AndroidManifest file. Make sure that its is singleTop or standard.

Hope this will work for you guys as well

like image 174
nadafafif Avatar answered Nov 10 '22 00:11

nadafafif


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

like image 36
HpTerm Avatar answered Nov 10 '22 01:11

HpTerm