23

Is it possible to manually call the method onCreateView in a Fragment or, if not, is there some way I can simulate this invocation?

I have a FragmentActivity with tabHost. Each tab contains a Fragment and I want to refresh the Fragment's view when I press the "Refresh" button. More specifically, I want to re-call the onCreateView method.

My code currently looks like:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
    view= inflater.inflate(R.layout.fragment_hall, container, false);

    layoutExsternal = (RelativeLayout) view.findViewById(R.id.layoutExsternal);
    layoutHall = (RelativeLayout) view.findViewById(R.id.layoutHall);

    init();

    return view;
 }

  [...]

@Override
public boolean onOptionsItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
     Log.d("itemSelected1", this.getClass().getSimpleName());

     switch (item.getItemId()) {
        case R.id.menu_refresh:

            //HERE I want to insert a method for refresh o redraw

     return true;
     }

return super.onOptionsItemSelected(item);

}
jwir3
  • 6,019
  • 5
  • 47
  • 92
Symon_9851
  • 375
  • 1
  • 3
  • 9
  • A better approach would be to refactor your onCreateView. First find your layout views and assign them to fields, then call a delegate method that populates them. Then have your refresh action call the delegate. You seem to already have this with your init() method. Can't refresh just call that? – k2col Aug 18 '16 at 16:42
  • I find this approach useful http://stackoverflow.com/a/41888950/3496570 – Zar E Ahmer Jan 27 '17 at 09:46

3 Answers3

35

Sometimes I found FragmentTransaction's replace would not work for replacing a fragment with itself, what does work for me is using detach and attach:

getSupportFragmentManager()
    .beginTransaction()
    .detach(fragment)
    .attach(fragment)
    .commit();

See this question for the difference between remove and detach

Community
  • 1
  • 1
Matthew Mcveigh
  • 5,695
  • 22
  • 22
  • I used it in `onActivityResult` (coming back from settings where layout-affecting options may have changed) where I needed `commitAllowingStateLoss` because "You will receive this call immediately before `onResume()` when your activity is re-starting.". Calling `commit` would cause "Can not perform this action after onSaveInstanceState". **Fragment state is still PRESERVED!** – TWiStErRob Sep 12 '14 at 13:23
  • 2
    Use .commitAllowingStateLoss() instead! – Flyview Apr 11 '15 at 19:32
9

I have resolved my question. I replace the current fragment with itself, but before I have saved a reference of current fragment and then i close life cycle of current fragment invoking onDestroy(). I recall it with "newFragment" variable.

         

     switch (item.getItemId()) {

        case R.id.menu_refresh:
         //THIS IS THE CODE TO REFRESH THE FRAGMENT.
             FragmentManager manager = getActivity().getSupportFragmentManager();
             FragmentTransaction ft = manager.beginTransaction();
             Fragment newFragment = this;
             this.onDestroy();
             ft.remove(this);
             ft.replace(container.getId(),newFragment);
              //container is the ViewGroup of current fragment
             ft.addToBackStack(null);   
             ft.commit();

        return true;
    }

Symon_9851
  • 375
  • 1
  • 3
  • 9
  • 5
    Manually calling lifecycle methods is never the right solution. Also you don't need the `newFragment` variable; you could just use `this` again. – Karu May 26 '14 at 03:38
  • As @Karu says, manually calling lifecycle methods is not a good approach. – jwir3 Jan 04 '15 at 01:47
3

You can just have your replace button replace the current layout with a new instance of the fragment.

// onButtonClick
SomeFragment fragment = new SomeFragment();
getFragmentManager().beginTransaction().replace(R.id.current_layout, fragment).commit();
loadedion
  • 2,217
  • 19
  • 41