Refresh fragment to select dialog fragment

I have a fragment that calls DialogFragment popups on a component. This portion of the dialog contains a list of options. When an option is selected from the list, I want to notify the fragment so that I can start the field update procedure. I did something like this

@Override public void onClick(DialogInterface dialog, int item) { updateSharedPreference(item); Log.e("ProfilePersonaListDialog", "Click on dialog, inside onClick"); OnCloseListDialogListener act = (OnCloseListDialogListener) getActivity(); act.onDialogListSelection(); dismiss(); } 

However, this getActivity () calls FragmentActivity, not the fragment that caused the dialog fragment. I can kill the current / running fragment and call a new instance that will receive the updated fields, but this is a dirty solution that I would prefer to avoid.

Any suggestions for updating the fragment after selecting an option in the dialog fragment of the dialog.

+7
source share
3 answers

Just come back with a solution. My problem was actually forwarding the current getTag () fragment as the show () parameter for DialogFragment. If someone is interested here, then the sample works.

Create a simple listener

 public interface OnCloseListDialogListener { public void onDialogListSelection(); } 

Create a new dialog box that will expand the DialogFragment dialog box

 public class ListDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { private PersonaData[] mPersonaData; private String[] mPersonaName; private final String TAG; public static ListDialogFragment newInstance(PersonaData[] personaData, String tag) { ListDialogFragment dialog = new ListDialogFragment(personaData, tag); Bundle bundle = new Bundle(); dialog.setArguments(bundle); return dialog; } private ListDialogFragment(PersonaData[] personaData, String tag) { this.mPersonaData = personaData.clone(); this.TAG = tag; } @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setCancelable(true); int style = DialogFragment.STYLE_NORMAL, theme = 0; setStyle(style, theme); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.dialog_title); mPersonaName = getData();//Your own implementation here builder.setNegativeButton("Cancel", this); builder.setSingleChoiceItems(mPersonaName, -1, new SingleChoiceListener()); return builder.create(); } @Override public void onClick(DialogInterface dialogInterface, int i) { } private class SingleChoiceListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int item) { updateSharedPreference(item); OnCloseListDialogListener act = (OnCloseListDialogListener) getFragmentManager().findFragmentByTag(TAG); act.onDialogListSelection(); dismiss(); } } } 

And then in the fragment from which you want to call this dialog box, do as shown below. DIALOG is just a String constant. I posted only a dialogue there

 SOME_CLICKABLE.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getFragmentManager(); ListDialogFragment dialog = ListDialogFragment.newInstance(mPersona, getTag()); dialog.show(manager, DIALOG); } }); 
+7
source

In most cases, it is necessary that a Fragment knows that it is running in the Activity context of some description and suppose that the child fragment calls a method on an interface implicitly implemented by the parent action (as shown in the listing of your code fragment). When you receive your recommendations, as Tomas points out, you will be gold.

However, to help reuse the fragment of the dialog, I suggest you use BroadcastReceiver s. BroadcastReceiver just broadcasts the message that I did "x". Parent activity or even any other top-level component can declare that I am listening to "x". As soon as the event is launched in the dialog component, this event will be collected by the parent action onReceive , where you can run the necessary code to update your fields.

On a personal level, I prefer this loose connection over the foundry interface because it makes me think about the purpose of each piece and keep it modular.

If you want to take a picture, read the section of the developer guide on BroadcastReceivers and follow these steps:

  • Deploy BroadcastReceiver to parent activity. Note that the onReceive method must be implemented.

  • Override the onResume parent activity onResume and register the action as the receiver of the event with the intent "blah". Something like:

      @Override protected void onResume() { super.onResume(); registerReceiver(this, new IntentFilter("blah")); } 
  • Cancel the onPause parental activity onPause , deregister the activity as a receiver in order to avoid "receiver leakage" (you'll find out).

     @Override protected void onPause() { super.onPause(); unregisterReceiver(deleteSpotReceiver); } 
  • In your DialogFragment onClick event that your parent activity β€œlistens” will light up.

     @Override public void onClick(DialogInterface dialog, int item) { updateSharedPreference(item); Log.e("ProfilePersonaListDialog", "Click on dialog, inside onClick"); final Intent intent = new Intent(); intent.setAction("blah"); getActivity().sendBroadcast(intent); dismiss(); } 

Parent activity will collect the message and you can continue processing. Let me know if you decide to accept this method.

+6
source

Just the way you did it above and added sth, like this in your activity:

 public void onDialogListSelection() { AnotherFragment anotherFragment = (AnotherFragment) getSupportFragmentManager() .findFragmentById(R.id.anotherFragment); anotherFragment.customMethodToNotifyListHasBeenSelected(); } 

Of course, if you are not using a support library, then call getFragmentManager instead of getSupportFragmentManager .

+1
source

All Articles