9

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();
        }
    });
richard
  • 724
  • 2
  • 14
  • 36
  • Post code and logcat output. – Squonk Aug 16 '13 at 11:28
  • 1
    i could fix my special problem by adding android:launchMode="standard" in the AndroidManifest.xml for the preference-activity (which before was - like the ListFragements - set to "singleInstance"). but the behavior of the startActivityForResult is still very strange for me. – richard Aug 16 '13 at 11:42
  • sorry, its the first time i add code, it took me a while. and now i have to find out of for format the logcat in the right way and add it. – richard Aug 16 '13 at 11:57
  • just a stackoverflow question: do i have to add 4 spaces before each line of the logcat info to make it look like code? is ther any other way how to format it? – richard Aug 16 '13 at 12:03
  • @Richard you can block quote it – HpTerm Aug 16 '13 at 12:05
  • possible duplicate of [onActivityResult() called prematurely](http://stackoverflow.com/questions/3354955/onactivityresult-called-prematurely) – HpTerm Aug 16 '13 at 12:11
  • @Richard thanks .Its working – sahu Mar 22 '17 at 06:26

4 Answers4

9

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

nadafafif
  • 561
  • 6
  • 10
4

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

Community
  • 1
  • 1
HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • thanks i will check if that helps me, but its definitely the same problem. sorry for my inexperienced questions concerning the handling of stackoverflow, but do i have to mark my question as duplicate now (if yes: how)? – richard Aug 16 '13 at 12:19
  • @Richard no problem, don't worry. At least you found you are not alone having the same trouble ;-). If you think I answered you can validate my answer so others know problem is answered. – HpTerm Aug 16 '13 at 12:22
0

onActivityResult, called after onResume,

The basic reason that when your jump from your activity to another activity without calling finish. Then last activity went in onStop state.

After onStop, onstart,onResume method call then onActivityResult called.

Jatin Bansal
  • 123
  • 8
0

onActivityResult called when .............suppose we have 2 activities and you go from one activity to another and then right now you are in 2nd activity and if you finish second activity then jump back to 1st activity then After onStop, onrestart, onstart, onResume method call then onActivityResult called.

  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 10 '21 at 11:12