0

I have the following Dialog progress. This dialog progress is being Injected into the activity/fragments as needed. When I display this in an Activity it works perfectly fine. But when I try to show it inside a fragment in an activity it shows up below the dialog.

Any way to fix this class to show the progress dialog correctly, without having to manually pass the context to this class?

Progress Dialog Class

public class ProgressBuilder implements IProgressBuilder {

    private ProgressDialog progressDialog;

    @Inject
    public ProgressBuilder(Provider<Context> contextProvider) {
        this.progressDialog = new ProgressDialog(contextProvider.get());
    }

    public void show() {
        this.show("Loading...", null);
    }

    public void show(String message) {
        this.show(null, message);
    }

    public void show(String title, String message) {
        this.progressDialog.setTitle(title);
        this.progressDialog.setMessage(message);
        this.progressDialog.show();
    }

    public void dismiss() {
        this.progressDialog.dismiss();
    }
}

Activity Class

public class MyActivity extends RoboFragmentActivity {

@Inject IProgressBuilder progress

}

This Fragment is shown on MainActivity

public class MyActivity extends RoboDialogFragment {

@Inject IProgressBuilder progress

}
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • You really should be using a DialogFragment for this. – Christopher Perry Dec 26 '12 at 00:49
  • @ChristopherPerry Can you provide an example please? I've read that on other threads already but no-one points how I could be using a DialogFragment for this reason – aryaxt Dec 26 '12 at 00:50
  • There's a ton of examples in the documentation: http://developer.android.com/reference/android/app/DialogFragment.html – Christopher Perry Dec 26 '12 at 00:51
  • @ChristopherPerry I've seen all those samples, but based on those samples the purpose of a DialogFragment is to display more content in as a modal on top of current activity, which I am already doing. Are you sure it's a good idea to create my own Progress as a subclass of a DialogFragment instead of using the default class provided by android? – aryaxt Dec 26 '12 at 00:53
  • You don't subclass DialogFragment with ProgressDialog, you wrap a ProgressDialog with a subclass of a DialogFragment. Use the example from the docs where they are wrapping an AlertDialog, and replace that with a ProgressDialog and you're set. You should always favor composition over inheritance. – Christopher Perry Dec 26 '12 at 00:57
  • @ChristopherPerry thanks for the response. Just add your comment as a response so that I can select it as best answer. I'm curious what the the reason behind not using a DialogBuilder directly? Is it no compatible with fragments? or is there any other reason? – aryaxt Dec 26 '12 at 01:01

1 Answers1

1

You really should be using a DialogFragment for this.

There's a ton of examples in the documentation:

developer.android.com/reference/android/app/DialogFragment.html

You would simply wrap a ProgressDialog with a subclass of a DialogFragment. Use the example from the docs where they are wrapping an AlertDialog, and replace that with a ProgressDialog and you're set.

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187