0

I have two Activities (A and B). Activity A consists of Fragment F. How can I pass a result from Activity B to my Fragment F? Here's my code:

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

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

My problem is, when I clicked the "to_acta" button, nothing is added in my edittext "text1".

general_bearbrand
  • 827
  • 5
  • 13
  • 29
  • 1
    Generally `OnActivityResult` does not execute in `Fragment`. So if your want to pass the result using onActivityResult then it will not execute that method. So even if you are passing the result it will not come in fragment as onActivityResult never executes. – GrIsHu Nov 11 '13 at 07:07
  • passing the result to my fragment f using the OnActivityResult.. – general_bearbrand Nov 11 '13 at 14:08
  • Try out starting activity as `getActivity(). startActivityForResult(intent, 1);` – GrIsHu Nov 19 '13 at 05:13
  • it may be not a good approach but you can set a static variable anywhere whether it is a fragment or an Activity.. – Zar E Ahmer Jun 17 '14 at 14:11
  • link may help http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Zar E Ahmer Jun 17 '14 at 14:13

1 Answers1

-1

replace

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

with

String hey = data.getStringExtra(ActivityB.EXTRA_MESSAGE);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Have you read the question carefully? Its asking to pass the result back in Fragment from activity using onActivityResult. And if you are aware `onActivityResult` does not executes in `Fragment`. – GrIsHu Nov 11 '13 at 07:12
  • @GrIsHu thanks for your suggestion... I am going to have a mess around it – Gopal Gopi Nov 11 '13 at 07:14
  • I don't know man, but your answer solved my problem. And I'm not the one who voted you down and I can't vote you up 'cos I don't have enough reputation to do so. By the way, the "startActivityForResult(intent, 1);" the 1 is the position of my ActivityB right? – general_bearbrand Nov 11 '13 at 07:22
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – ѕтƒ Nov 11 '13 at 07:24
  • @user2942600 No. 1 is request code to the ActivityB and it can be any non negative and non zero integer. and if you can't vote up, but you can accept answerby clicking the tick mark near to my answer – Gopal Gopi Nov 11 '13 at 07:25
  • @ѕтƒ - ok sir sorry for that – general_bearbrand Nov 11 '13 at 07:26
  • @GrIsHu why does `onActivityResult` does not executes in `Fragment`? – Raghunandan Nov 18 '13 at 15:21
  • 2
    @user2942600 you have not accepted any of the answers provided to the questions you asked. Does non of them help you? – Raghunandan Nov 18 '13 at 15:23
  • @GrIsHu onActivityResult() will be called in Fragment also. once check that yourself... – Gopal Gopi Dec 24 '13 at 03:22
  • The people who downvoted it, be careful when you are downvoting, even the fellow who asked the question, said it helped but still it is downvoted... – Gopal Gopi Dec 24 '13 at 03:24