简体   繁体   中英

startactivityforresult() inside a listener not calling onActivityResult()

I know there are many similar questions on SO, but none of them resolved my issue.

I am calling startActivityForResult inside a seekbar listener. However, onActivityResult is not getting called. Here is my code. What could be going wrong?

public class PeopleActivity extends SherlockFragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progressChanged = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progressChanged = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


                Intent in = new Intent(getActivity(), com.mytest.mytestninja.RangingActivity.class);
                getActivity().startActivityForResult(in, 1);


            }

        });


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

        rangeValue.setText("CALLED");
    }

}

RangingActivity.java

public class RangingActivity extends Activity
@Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ranging);

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", firstBeacon.toString());
            setResult(RESULT_OK, returnIntent);
            finish();
 }
}

UPDATE

Activity in which PeopleActivity fragment is used :

public class DrawerFrontActivity extends SherlockFragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bar = getSupportActionBar();
        Drawable myIcon = getResources().getDrawable( R.drawable.bluebar);
        bar.setBackgroundDrawable(myIcon);

        SherlockFragment fragment;
        fragment = new PeopleActivity();

        FragmentTransaction transaction =    getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.rlDrawerHome, fragment);       
        transaction.commit();

        setTitle("Home");
        bar.setTitle("Home");
}
}

It seems you're calling getActivity().startActivityForResult(in, 1); which would pass result back to activity and not fragment it self.

Try using PeopleActivity.this.startActivityForResult(in, 1); , this should pass result back to fragment.

Refer to this answer for more details: https://stackoverflow.com/a/6147919/2146871

Found the answer. The issue was because I was not calling super.onactivityresult in my fragment activity DrawerFrontActivity. Hence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM