1

I created a progressdialog following the new "fragment way" with this code:

public class DialogUpdateTrackRecords extends DialogFragment {

    public static DialogUpdateTrackRecords newInstance() {
        DialogUpdateTrackRecords frag = new DialogUpdateTrackRecords();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
    }

    @Override
    public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
        this.setCancelable(false);
        ProgressDialog dialog= new ProgressDialog(getActivity());
        dialog.setTitle("Caricamento tragitti");
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Sending something");

        return dialog;
    }
}

and I show it from an activity like this way:

FragmentManager fm= getSupportFragmentManager();
uploadDialogFrament= (DialogFragment) getSupportFragmentManager().findFragmentByTag("sendDialog");
if(uploadDialogFrament!=null)
    uploadDialogFrament.dismiss();
FragmentTransaction ft= fm.beginTransaction();

uploadDialogFrament= DialogUpdateTrackRecords.newInstance();
uploadDialogFrament.show(ft,"sendDialog");
fm.executePendingTransactions();
((ProgressDialog)uploadDialogFrament.getDialog()).setMax(trackRecordSize);
if( trackRecordSize > 1 )
    ((ProgressDialog)uploadDialogFrament.getDialog()).setIndeterminate(false);

As you can see, I get a reference to the dialog and I set it (depending on my needs). Everything works like a charm but.. if I rotate the device, the dialog goes back to its pristine state instead of retaining (in the example: the bar is set back to an indefinite state) my new settings. I checked if I was creating and displaying a new dialog erroneously, but this is not the case. So.. how could I keep my changes over the recreation of the activity?

Bertuz
  • 2,390
  • 3
  • 25
  • 50
  • I forgot to say that I'm using a SherlockFragmentActivity. I think it shouldn't affect my fragment retainment but.. who knows? – Bertuz Jun 11 '13 at 01:08

2 Answers2

1

Have you tried

setRetainInstance(true)

Since the DialogFragment extends the base Fragment, im pretty sure that will work.

Shaun
  • 5,483
  • 10
  • 40
  • 49
  • 1
    Yep! I put a `setRetainInstance(true)` in the Fragment's `onCreate` function. Guess what? If set, and you rotate the screen, THE FRAGMENT DISAPPEARS (I mean: the dialog disappears from the screen. Not sure if the dialog is still there). So funny, huh? – Bertuz Jun 11 '13 at 01:11
  • So.. the good news is that I found this that solves the "setretaininstance" part [link](http://stackoverflow.com/questions/12433397/android-dialogfragment-disappears-after-orientation-change). BAD NEWS: even though "setretaininstance" is set to true, the dialog does not retain its new settings after its creation in onCreateDialog. – Bertuz Jun 11 '13 at 01:44
  • 2
    Well, then use the onSavedInstanceState() method, create a bundle, save each value for the views that you have (the edit texts, etc) and then use super.onSavedInstanceState(bundle). Then, on the onCreate method, get the values from the bundle and put them back into the views – Shaun Jun 11 '13 at 02:00
  • I think I was writing that workaround in the meanwhile you were commenting right here :) The workaround works, but this is not absolutely the behavior we could expect. I think it's a support-v4 bug (what a news I'd say) – Bertuz Jun 12 '13 at 01:29
0

OK.. this is simply a workaround and I DO NOT SUGGEST IT. Notwithstanding the displaimer: I really don't have the necessary time to dig into the logic of this bug and I used this simple workaround. To make it short: each time I resume the dialog fragment, I update it with all the settings it should automatically have. Here's an example:

public class DialogUpdateTrackRecords extends DialogFragment {
    private boolean indeterminate= true;

    public static DialogUpdateTrackRecords newInstance() {
        DialogUpdateTrackRecords frag = new DialogUpdateTrackRecords();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

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

        ((ProgressDialog)getDialog()).setIndeterminate(indeterminate);
    }

    @Override
    public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
        this.setCancelable(false);
        ProgressDialog dialog= new ProgressDialog(getActivity());
        dialog.setTitle("Caricamento tragitti");
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Invio delle statistiche completate a ISF Modena in corso");
        return dialog;
    }


    public void setIndeterminate(){
        ((ProgressDialog)getDialog()).setIndeterminate(false);
        indeterminate= false;
    }



    /*
    [italian soh]
    http://stackoverflow.com/questions/12433397/android-dialogfragment-disappears-after-orientation-change
    thanks, google. Thanks.
    [/italian soh]
     */
    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);
        super.onDestroyView();
    }

}

Anyway: if anybody finds the reason of that weird behavior I would be more than happy to fix my code properly.

Bertuz
  • 2,390
  • 3
  • 25
  • 50