Code example for Fragment

Methods: getActivity, getFragmentManager

0
        } 
    } 
 
    private void showProgressDialog() { 
        DialogFragment progressDialog =
                (DialogFragment) getFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG);
        if (progressDialog == null) {
            progressDialog = ProgressDialogFragment.create();
            progressDialog.show(getFragmentManager(), TAG_PROGRESS_DIALOG);
        } 
    } 
 
    public static final class ProgressDialogFragment extends DialogFragment {
 
        private static final String ARG_MESSAGE = "message";
 
        public static ProgressDialogFragment create(int message) {
            ProgressDialogFragment progressDialogFragment = new ProgressDialogFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_MESSAGE, message);
            progressDialogFragment.setArguments(args);
            return progressDialogFragment;
        } 
 
        public static ProgressDialogFragment create() { 
            return create(R.string.progress_message); 
        } 
 
        @Override 
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage(getString(getArguments().getInt(ARG_MESSAGE)));
            return progressDialog;
        } 
 
        @Override 
        public void onCancel(DialogInterface dialog) {
            super.onCancel(dialog);
            FragmentActivity activity = getActivity();
            if (activity == null) {
                return; 
            } 
 
            Fragment fragment =
                    activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
            if (fragment instanceof PlusClientFragment) {
                ((PlusClientFragment) fragment).onDialogCanceled(getTag());
            } 
        } 
 
        @Override 
        public void onDismiss(DialogInterface dialog) {
            super.onDismiss(dialog);
            FragmentActivity activity = getActivity();
            if (activity == null) {
                return; 
            } 
 
           Fragment fragment =
                    activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
            if (fragment instanceof PlusClientFragment) {
                ((PlusClientFragment) fragment).onDialogDismissed(getTag());
            } 
        } 
    } 
 
    protected void hideProgressDialog() { 
        mRequestCode = INVALID_REQUEST_CODE;
        DialogFragment progressDialog =
                (DialogFragment) getFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG);
        if (progressDialog != null) {
            progressDialog.dismiss();
        } 
    } 
 
    private void showErrorDialog(DialogFragment errorDialog) {
        DialogFragment oldErrorDialog =
                (DialogFragment) getFragmentManager().findFragmentByTag(TAG_ERROR_DIALOG);
        if (oldErrorDialog != null) {
            oldErrorDialog.dismiss();
        } 
 
        hideProgressDialog(); 
        errorDialog.show(getFragmentManager(), TAG_ERROR_DIALOG);
    } 
 
    private void hideErrorDialog() { 
        mRequestCode = INVALID_REQUEST_CODE;
        DialogFragment errorDialog =
                (DialogFragment) getFragmentManager().findFragmentByTag(TAG_ERROR_DIALOG);
        if (errorDialog != null) {
            errorDialog.dismiss();
        } 
    } 
 
    private void startResolution() { 
        try { 
            mLastConnectionResult.startResolutionForResult(getActivity(), mRequestCode);
        } catch (SendIntentException e) {
            // The intent we had is not valid right now, perhaps the remote process died. 
            // Try to reconnect to get a new resolution intent. 
            mLastConnectionResult = null;
            showProgressDialog();