3

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

Menma
  • 799
  • 1
  • 9
  • 35

2 Answers2

2

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.

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
  • I'm sorry kishan.. I still don't get it, Can you tell me how to implement your method in my case (in fragment transition) please? – Menma Sep 15 '14 at 04:41
  • Call showSimpleProgressDialog() when you want to show dialog and now Call removeSimpleProgressDialog() method to your TertanggungPolis's Oncreate method. – Kishan Dhamat Sep 15 '14 at 04:44
0

use Asynch Task http://developer.android.com/reference/android/os/AsyncTask.html to show progress dialog while in transition.

Rudra
  • 241
  • 1
  • 3
  • 9