startactivityforresult()在监听器内部没有调用onActivityResult()

时间:2015-10-15 11:43:05

标签: android android-fragments start-activity

我知道SO上有很多类似的问题,但没有一个能解决我的问题。

我在seekbar监听器中调用startActivityForResult。但是,onActivityResult没有被调用。这是我的代码。可能出现什么问题?

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();
 }
}

更新

使用PeopleActivity片段的活动:

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");
}
}

2 个答案:

答案 0 :(得分:2)

您似乎正在调用getActivity().startActivityForResult(in, 1);,这会将结果传回活动而不会将其自行分割。

尝试使用PeopleActivity.this.startActivityForResult(in, 1);,这应该将结果传递回片段。

有关详细信息,请参阅此答案: https://stackoverflow.com/a/6147919/2146871

答案 1 :(得分:0)

找到答案。 问题是因为我没有在我的片段活动DrawerFrontActivity中调用super.onactivityresult。因此