Code example for Fragment

Methods: getActivity, isResumed

0
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) {
        mLastConnectionResult = connectionResult;
        mIsConnecting = false;
        // On a failed connection try again. 
        if (isResumed() && mRequestCode != INVALID_REQUEST_CODE) {
            resolveLastResult(); 
        } 
    } 
 
    @Override 
    public void onAccessRevoked(ConnectionResult status) {
        // Reconnect to get a new mPlusClient. 
        mLastConnectionResult = null;
        // Cancel sign in. 
        mRequestCode = INVALID_REQUEST_CODE;
 
        // Reconnect to fetch the sign-in (account chooser) intent from the plus client. 
        mPlusClient.connect();
    } 
 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Let new activities know the signed-in state. 
        if (mPlusClient.isConnected()) {
            mHandler.sendEmptyMessage(PlusClientFragmentHandler.WHAT_SIGNED_IN);
        } 
    } 
 
    @Override 
    public void onDisconnected() { 
        mIsConnecting = false;
    } 
 
    /** 
     * Shows any UI required to resolve the error connecting. 
     */ 
    public void signIn(int requestCode) {
        if (requestCode < 0) {
            throw new IllegalArgumentException("A non-negative request code is required.");
        } 
 
        if (mPlusClient.isConnected()) {
            // Already connected!  Schedule callback. 
            mHandler.sendEmptyMessage(PlusClientFragmentHandler.WHAT_SIGNED_IN);
            return; 
        } 
 
        if (mRequestCode != INVALID_REQUEST_CODE) {
            // We're already signing in. 
            return; 
        } 
 
        mRequestCode = requestCode;
        if (mLastConnectionResult == null) {
            // We're starting up, show progress. 
            showProgressDialog(); 
            return; 
        } 
 
        resolveLastResult(); 
    } 
 
    /** 
     * Perform resolution given a non-null result. 
     */ 
    private void resolveLastResult() { 
        if (GooglePlayServicesUtil.isUserRecoverableError(mLastConnectionResult.getErrorCode())) {
            // Show a dialog to install or enable Google Play services. 
            showErrorDialog(ErrorDialogFragment.create(mLastConnectionResult.getErrorCode(),
                    mRequestCode));
            return; 
        } 
 
        if (mLastConnectionResult.hasResolution()) {
            startResolution(); 
        } 
    } 
 
    public static final class ErrorDialogFragment extends GooglePlayServicesErrorDialogFragment { 
 
        public static ErrorDialogFragment create(int errorCode, int requestCode) {
            ErrorDialogFragment fragment = new ErrorDialogFragment();
            fragment.setArguments(createArguments(errorCode, requestCode));
            return fragment;
        } 
 
        @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());
            } 
        } 
    } 
 
    private void onDialogCanceled(String tag) {
        mRequestCode = INVALID_REQUEST_CODE;
        hideProgressDialog(); 
    } 
 
    private void onDialogDismissed(String tag) {
        if (TAG_PROGRESS_DIALOG.equals(tag)) {
            mRequestCode = INVALID_REQUEST_CODE;
            hideProgressDialog(); 
        } 
    } 
 
    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();