2

I have a fragment that throws an activity in which user can insert some details. When the user terminate to fill data the activity close. Then the previous fragment is showed and I want it to be updated (with the data provided by user in the activity).

If it was an activity, instead of fragment, i could use this:

@Override
public void onResume(){
    super.onResume();
    recreate();
}

How to do the same with fragment?

smartmouse
  • 13,912
  • 34
  • 100
  • 166

2 Answers2

1

You would override onResume in the Activity which this Fragment belong, and recreate (restarted) this Fragment in this Activity. For example

public class ActivityA extends Activity {
    @Override
    public void onResume() {
        //  here you can get the Fragment  instance , so you can recreated this fragment      here or invoke this fragment's function to set data or refresh data
    }
}
jerry
  • 484
  • 1
  • 5
  • 17
0

You can throw an Activity in calling startActivityForResult (Intent intent, int requestCode) method in your fragment.

also you need to provide onActivityResult (int requestCode, int resultCode, Intent data) in your fragment,the data is in the Intent parameter.

public class YourFragment extends Fragment {

     public void throwActivity() {
            // start activity
            Intent intent = new Intent();
            intent.setClass(getActivity(), YourActivity.class);
            int requestCode = 1;
            startActivityForResult(intent,requestCode);
        }

        // callback
        @Override
        public void onActivityResult (int requestCode, int resultCode, Intent data) {

            recreate();

        }

        public void recreate() {
            // your refresh code
        }
}
public class YourActivity extends Activity {

    public void yourMethod() {

           int resultCode = 2;
           setResult(resultCode);    // goback to your fragment with your data
        }
}
tounaobun
  • 14,570
  • 9
  • 53
  • 75