4

This is my dialog inside fragment it works fine, now I want that when i click "ok" button it reload the current fragment. the dialog is show when the method showDialog is called: mi fragment is android.support.v4.app.Fragment

void showDialog() {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    View promptView = layoutInflater.inflate(R.layout.dialog_fragment_agenda, null);
    TextView txtNombre = (TextView)promptView.findViewById(R.id.txtdialog1);
    txtNombre.setText("ADD THIS STUFF?");
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
    alertDialogBuilder.setView(promptView);

    alertDialogBuilder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            sendSomeStuff();
            //HERE TODO RELOAD OR REFRESH THE FRAGMENT


        }
    });
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        } });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

update this is the fragment. Iwant to recall the onCreateView methos of the fragment

public class FragmentOne extends Fragment {
//...some variables
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_one, container, false);
    showDialog()//HERE I CALL MY CUSTOM DIALOG

    return rootView;
}
}

The Simple Solution

Tested using viewpager, and FragmentPagerAdapter

FragmentTransaction ft = getFragmentManager().beginTransaction();

ft.detach(FragmentOne.this).attach(FragmentOne.this).commit();
matQ
  • 597
  • 2
  • 13
  • 27
  • can we also have the two .xml files pls? Do you have a SwipeRefreshLayout on your Fragment? – Basti Mar 29 '18 at 19:46
  • please explain how excatly you want to reload the fragment. If you want to fully recreate the fragment (which is not recomended) you can make the fragment transaction again with same view and new fragment instance. If you just want to update fragments contents you can just update your views depending on your fragment's state – GV_FiQst Mar 29 '18 at 19:48
  • I want to recrreate the view or recall the onCreateView method of the fragment – matQ Mar 29 '18 at 19:50
  • @GV_FiQst I add the fragment code – matQ Mar 29 '18 at 19:56
  • 1
    Sorry, but your logic is complex, and not recommended. Please reconsider what you are trying to do. What is your actual issue? Perhaps a simpler solution can be applied – rgv Mar 29 '18 at 19:58
  • Is complex? to refresh the fragment form a dialog when click "ok"? there are some autocompletetetview adapter that are filled with information of external server. after click ok button from dialog, the adapter must be rebuilt because the json will not be the same – matQ Mar 29 '18 at 20:02

2 Answers2

1

refresh fragment when fragment is visible to user

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)
    if(isVisibleToUser){
        if (getFragmentManager() != null) {
            getFragmentManager()
                ?.beginTransaction()
                ?.detach(this)
                ?.attach(this)
                ?.commit();
        }
    }
}
Sanjib Suna
  • 284
  • 12
  • 18
0

There is no standart way to recreate the fragment. Fragment has no recreate() method like Activity has. Unlike Activities, instances of which are created by system, fragments are created by developer (means you). So you have two ways:

Way #1 Recreate the fragment instance (Not recommended)

If you want to have onCreateView() method being called again you can run the same transaction again to recreate the fragment instance:

FragmentOne fg = new FragmentOne();
fg.setArguments(yourArgsBundle);

getFragmentManager()  // or getSupportFragmentManager() if your fragment is part of support library
        .beginTransaction()
        .replace(R.id.yourRootView, fg)
        .commit();

This way is not recommended because this will cause your content view to be reinflated, and view-tree to be rebuilt again.

Way #2 Update the current fragment instance

Much simpler and more recommended way is to simply update your view depending on your new json:

If it's aTextView where the text was changed call myTextView.setText("new data"); again. If it's anImageView where source image was changed call myImageView.setImageBitmap(myNewBitmap) again.

You have initialized your view depending on your json once for sure. So just do this again with new json.

P.S. don't use onCreateView() method in initialization purposes. Better use onViewCreated(View view, Bundle savedState) method in these purposes.

GV_FiQst
  • 1,487
  • 14
  • 30