Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retain listener on custom dialog opened from fragment?
    primarykey
    data
    text
    <p>I have run into a bit of a roadblock. I have a scenario VERY similar to the one described at: <a href="https://stackoverflow.com/questions/14042495/dialogfragment-retaining-listener-after-screen-rotation">DialogFragment - retaining listener after screen rotation</a></p> <p>The proposed solution works fine for the author because his dialog is being called from an activity. My case is the exact same, but my Custom Dialog is called from a Fragment instead of an Activity. (IE Activity->Fragment->Dialog)</p> <p>I implemented the same solution (setting the listener in onResume from the calling Fragment) but it doesn't work in this case.</p> <p>What seems to be happening is that when the screen is rotated, Android kills the Dialog and Fragment. Then recreates them IN THAT ORDER. So when my onCreateDialog is called on my custom dialog the containing Fragment is yet to be recreated, so it has null for the listener to set for positive and negative buttons.</p> <p>Does anyone know a way around this?</p> <p>I can post code if anyone thinks it would be necessary, but it's pretty much the same as the code on the linked thread.</p> <p>UPDATED WITH CODE:</p> <pre><code>public class RecipeDetailEditFragment extends SherlockFragment implements DialogInterface.OnClickListener { private EditStepFragmentDialog stepDialog; private Recipe newRecipe; //main data object implements parcelable ... public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... stepDialog = EditStepFragmentDialog.newInstance(newRecipe); //I've also tried passing 'this' into the newInstance constructor and //setting the listener there, but that doesn't work either } public void onResume() { stepDialog.setListener(this); super.onResume(); } ... } public class EditStepFragmentDialog extends DialogFragment { private DialogInterface.OnClickListener ocl; private static final String ARG_RECIPE = "recipe"; private Recipe recipe; public EditStepFragmentDialog() {} public static EditStepFragmentDialog newInstance(Recipe rec) { //(Recipe rec, DialogInterface.OnClickListener oc) as mentioned doesn't work. EditStepFragmentDialog dia = new EditStepFragmentDialog(); Bundle args = new Bundle(); args.putParcelable(ARG_RECIPE, rec); //dia.setListener(oc); return dia; } public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); if (getArguments().containsKey(ARG_RECIPE)) { recipe = (Recipe) getArguments().getParcelable(ARG_RECIPE); } ... adb.setPositiveButton("Done", ocl); adb.setNegativeButton("Cancel", ocl); ... return adb.create(); } public void setListener(DialogInterface.OnClickListener cl) { ocl = cl; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload