Search code examples
androidandroid-fragmentsfragmenttransaction

add progress dialog while fragment transition


I try to show A ProgressDialog while switching one Fragment to another Fragment such as message "please wait..." or "loading page..." etc I need to add this message because loading time of Fragment even I have added getSupportFragmentManager().executePendingTransactions(); after commit my Fragment transition, I have try as below :

    ProgressDialog pd=    ProgressDialog.show(getActivity(), "Please Wait","loading page...");   
    getFragmentManager().beginTransaction().replace(R.id.frame_container, new TertanggungPolis()).
    setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
    pd.dismiss();

But this code seems not working, becase there's no ProgressDialog that shown in my application, so is there something wrong with my code or my method? thank you


Solution

  • Use this:

    import android.app.ProgressDialog;
    import android.content.Context;
    
    public class AndyUtills {
    
        private static ProgressDialog mProgressDialog;
    
    
        public static void showSimpleProgressDialog(Context context, String title,
                                                    String msg, boolean isCancelable) {
            try {
                if (mProgressDialog == null) {
                    mProgressDialog = ProgressDialog.show(context, title, msg);
                    mProgressDialog.setCancelable(isCancelable);
                }
    
                if (!mProgressDialog.isShowing()) {
                    mProgressDialog.show();
                }
    
            } catch (IllegalArgumentException ie) {
                ie.printStackTrace();
            } catch (RuntimeException re) {
                re.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void removeSimpleProgressDialog() {
            try {
                if (mProgressDialog != null) {
                    if (mProgressDialog.isShowing()) {
                        mProgressDialog.dismiss();
                        mProgressDialog = null;
                    }
                }
            } catch (IllegalArgumentException ie) {
                ie.printStackTrace();
    
            } catch (RuntimeException re) {
                re.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    

    Now call both of these method using className itself and your work is done. call show method when you want to show dialog and remove method when you want to remove dialog.

    Now In your Case you can call remove method in second fragment's OnCreateView Method.