0

我有两个活动(A 和 B)。Activity A 由 Fragment F 组成。如何将 Activity B 的结果传递给 Fragment F?这是我的代码:

片段 F

public class FragmentF extends Fragment implements OnCLickListener {

// my editText is in this fragmentf layout
EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragmentf, container, false);            
        Button b = (Button) v.findViewById(R.id.to_actb);
        b.setOnClickListener(this); 
        return v;
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(getActivity(), ActivityB.class);
        startActivityForResult(intent, 1);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, intent);

          if (requestCode == 1) {

             if(resultCode == ActivityB.RESULT_OK){      
                 String hey = getActivity().getIntent().getStringExtra("hey.hello.MESSAGE");
                 EditText editText = (EditText) getActivity().findViewById(R.id.text1);
                 editText.setText(hey); 
             }
          }
    }
}

活动 B

public class ActivityB extends Activity {

    public final static String EXTRA_MESSAGE = "hey.hello.MESSAGE";

....

    public void to_acta(View view) {

        Intent intent = new Intent(this, ActivityA.class);
        TextView textView = (TextView) findViewById(R.id.text);
        String message = textView.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        setResult(RESULT_OK,intent);
        finish();      
    }

我的问题是,当我单击“to_acta”按钮时,我的编辑文本“text1”中没有添加任何内容。

4

1 回答 1

-2

代替

String hey = getActivity().getIntent().getStringExtra("hey.hello.MESSAGE");

String hey = data.getStringExtra(ActivityB.EXTRA_MESSAGE);
于 2013-11-11T07:06:18.537 回答