Home » Android » Android DialogFragment progress bar

Android DialogFragment progress bar

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve searched everywhere and I can’t find a solution to this problem.

Basically I have a login screen and I’m trying to get a progress spinner to show up while it’s logging in to the server (via a thread), and then dismiss it after the login is successful. It has to work while changing orientations.

I am using DialogFragment with the Android compatibility package to make a progress bar (can’t find any documentation on it, only for basic\alert dialog) because showDialog() is deprecated now. Right now I just show a custom message box as a login spinner.

In Summary:

  • How can I set up a Progress spinner with DialogFragment.
  • How can I dismiss it in another thread after orientation changes.
How to&Answers:

I know this is old question but I want to share much better solution for this

According to Android Development Protip:

“Stop using ProgressDialog,Inline indicators are your friend”

As Roman Nurik states:

enter image description here

This one’s quick. Stop using ProgressDialog and other modal loading
indicators. They’re extremely interruptive and annoying, especially
when:

  • You see one every time you switch tabs.

  • You can’t Back out of them.

  • They say “Please wait.” No thanks, I’d rather just uninstall.

Either show loading indicators inline with your content (e.g.
http://developer.android.com/training/animation/crossfade.html) or better yet, load small amounts of data in the
background so that you minimize the need to even show a loading
indicator.

More about progress & activity in the design guidelines.

Answer:

For showing a progress spinner, just override DialogFragment.onCreateDialog() in your dialog fragment like this (no need for overriding onCreateView()):

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final ProgressDialog dialog = new ProgressDialog(getActivity());
    //
    dialog.setTitle(R.string.login_title);
    dialog.setMessage(getString(R.string.login_message));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    // etc...
    return dialog;
}

As for dismissing that dialog fragment from somewhere else, you’ll need to get a hold of FragmentManager (from inside your next FragmentActivity or Fragment) and call popBackStack() on it (if you don’t do any other fragment transaction in the meantime).

If there’s more steps/fragment transactions between your progress dialog fragment and the next activity, you’ll probably need one of the other popBackStack(...) methods that take an ID or tag to pop everything up to your progress dialog fragment off the stack.