0

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");
}
}
Ninja
  • 5,082
  • 6
  • 37
  • 59
  • Post the setResult() code of your target class, i.e. RangingActivity. – Thomas R. Oct 15 '15 at 11:45
  • Updated with the target class. I am leaving out default methods like OnPause, OnDestroy etc, since I am not making any changes to those functions. – Ninja Oct 15 '15 at 11:50
  • Finishing in onCreate()? Afaik this will corrupt the lifecycle of the activity. Can you add a button and test if it works when you click the button? Or maybe you can try to perform this snippet in onResume(). – Thomas R. Oct 15 '15 at 11:53

2 Answers2

2

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

Community
  • 1
  • 1
JuliusScript
  • 3,850
  • 1
  • 17
  • 17
  • This didn't work. I tried with PeopleActivity.this.startActivityForResult(in, 1); – Ninja Oct 15 '15 at 12:47
  • Could you post code of an activity in which "PeopleActivity" fragment is used? – JuliusScript Oct 15 '15 at 12:56
  • I have updated the question with the code snippet you requested. – Ninja Oct 15 '15 at 13:11
  • Also I guess you could post manifest to help others help you. – JuliusScript Oct 15 '15 at 13:11
  • 1
    I've created a quick copy project from code you have posted. https://github.com/CityVibes/TempActStartTest it appears that project with my fix does work. Try to compare code with your own project, but for now I'll assume issue is in other parts of your code that's not posted here. – JuliusScript Oct 15 '15 at 14:17
  • Well, this is quite frustrating. I plugged in the same code (check gist here https://gist.github.com/niranjan-uma-shankar/7c99d3c17e9f91c86eda), and yet the onActivityResult function is not getting called. My RangeActivity class is same as the one you created. What else could be going wrong? – Ninja Oct 15 '15 at 15:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92408/discussion-between-julius-skripkauskas-and-ninja). – JuliusScript Oct 15 '15 at 16:12
0

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

Ninja
  • 5,082
  • 6
  • 37
  • 59