I know that Google's Material Design guidelines don't recommend using a ProgressDialog, instead using another less intrusive way to display progress, but I need to use a ProgressDialog for a specific Activity of my app.
So, the thing is that I want to wrap a ProgressDialog inside a DialogFragment, and thus my code is as follows:
public class MaterialProgressDialogFragment extends DialogFragment {
private int total;
private MaterialDialog myDialog;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return myDialog;
}
public void incrementProgress(int by) {
myDialog.incrementProgress(by);
}
public void setTotal(int total) {
this.total = total;
}
public void setUp(Context context) {
myDialog = new MaterialDialog.Builder(context)
.title("Progress")
.content("Processing...")
.progress(false, total, true)
.build();
}
}
Because what I want to build is a determinate ProgressDialog, I want to be able to update its progress throghout the life of my app. For this I have made a method called setProgress(progress), but myDialog is always null, as well as the value returned from getDialog().
What am I doing wrong?
Thank you
EDIT: I'm showing the dialog inside my fragment's onCreateActivity() method, like follows:
MaterialProgressDialogFragment dialogFragment = new MaterialProgressDialogFragment();
dialogFragment.setTotal(100);
dialogFragment.setUp(getActivity());
dialogFragment.show(getSupportFragmentManager(), "");
dialog.incrementProgress(50);
Everything works as expected until the last line, which causes the app to throw an exception.
It isn't totally clear what the variable
dialog
insidesetProgress(int progress)
method is. But if you meangetDialog()
by that, it takes time to create dialog by dialog fragment, and between thedialogFragment.show(getSupportFragmentManager(), "")
andonCreateDialog(Bundle savedInstanceState)
callback there will be some time interval, during whichgetDialog()
will returnnull
.EDIT: Ok, now it is more clear. But by the code above, you're violating the fragment framework rules. You should create your dialog in
onCreateDialog(Bundle savedInstanceState)
method, because otherwise you'll have problems with lifecycle (for example, if you'll rotate your screen, the app will crash).I suggest you to use something like that:
You should save
total
variable to arguments, because it will be destroyed on configuration change (for example screen rotation).Then just create and show it by:
When you want to change your progress, call:
But remember, dialog won't be created immediately, so if you'll call this right after show(), it won't take effect, because
getDialog()
will return null. If you want to just test it, call it delayed:But anyway in real app you will change your progress from some background process.