0

I have fragment, from that fragment i go to 1 activity and do some calculation then i press back button and that value again pass to that fragment so how i can do this?

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • Possible duplicate of [Send data from activity to fragment in android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Hemant Parmar Jun 07 '18 at 10:24

2 Answers2

0

Hey you can use onActivityResult() of fragment and in onBackpress() of activity pass the data in setResult().

I hope this will help you.

Manish Karena
  • 724
  • 6
  • 29
kundan kamal
  • 674
  • 7
  • 16
0

When you start activity from fragment you can start as below:-

   startActivityForResult(new Intent(context,NewActivity.class),1);

And when you pressed back button then onBakcPressed method

Intent intent = new Intent();
intent.putExtra("data", "value_here")
setResult(RESULT_OK, intent);

In your fragment override onActivityResult method :-

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK) {
             String strEditText = data.getStringExtra("data");
         }     
    }
} 
Anand Jain
  • 2,365
  • 7
  • 40
  • 66