Example usage for android.app Activity setResult

List of usage examples for android.app Activity setResult

Introduction

In this page you can find the example usage for android.app Activity setResult.

Prototype

public final void setResult(int resultCode, Intent data) 

Source Link

Document

Call this to set the result that your activity will return to its caller.

Usage

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.FavouriteStopsFragment.java

/**
 * {@inheritDoc}//from  ww w  .j a va 2s  .  c o  m
 */
@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
    // Get the stopCode and cache the Activity.
    final String stopCode = String.valueOf(id);
    final Activity activity = getActivity();
    Intent intent;

    // What happens when the user selects a list item depends on what mode
    // is active.
    if (isCreateShortcut) {
        // Set the Intent which is used when the shortcut is tapped.
        intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(activity, DisplayStopDataActivity.class);
        intent.setAction(DisplayStopDataActivity.ACTION_VIEW_STOP_DATA);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(DisplayStopDataActivity.ARG_STOPCODE, stopCode);

        // Set the Activity result to send back to the launcher, which
        // contains a name, Intent and icon.
        final Intent result = new Intent();
        result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        result.putExtra(Intent.EXTRA_SHORTCUT_NAME, sd.getNameForStop(stopCode));
        final Parcelable icon = Intent.ShortcutIconResource.fromContext(activity, R.drawable.appicon_favourite);
        result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

        // Set the Activity result and exit.
        activity.setResult(Activity.RESULT_OK, result);
        activity.finish();
    } else {
        // View bus stop times.
        callbacks.onShowBusTimes(stopCode);
    }
}

From source file:com.mysampleapp.camera.Camera2BasicFragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *///from  w  w w  . jav a 2  s .co  m
private void captureStillPicture() {
    try {
        Log.d(TAG, "In still capture");
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                //showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                unlockFocus();

                //sending to activity
                Intent imageData = new Intent();
                imageData.putExtra("data", mFile.toString());
                imageData.putExtra("isMale", isMale);
                Log.d(TAG, "Sent data as gender: " + isMale);
                activity.setResult(ResultActivity.RESULT_OK, imageData);
                activity.finish();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:org.dmfs.tasks.EditTaskFragment.java

/**
 * Persist the current task (if anything has been edited) and close the editor.
 *///from   w  w w  . j ava2 s.  co  m
public void saveAndExit() {
    // TODO: put that in a background task
    Activity activity = getActivity();

    int resultCode = Activity.RESULT_CANCELED;
    Intent result = null;
    int toastId = -1;

    if (mEditor != null) {
        mEditor.updateValues();
    }

    if (mValues.isInsert() || mValues.isUpdate()) {
        if (TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues))) {
            // there is no title, try to set one from the description or check list

            String description = TaskFieldAdapters.DESCRIPTION.get(mValues);
            if (description != null) {
                // remove spaces and empty lines
                description = description.trim();
            }

            if (!TextUtils.isEmpty(description)) {
                // we have a description, use it to make up a title
                int eol = description.indexOf('\n');
                TaskFieldAdapters.TITLE.set(mValues, description.substring(0, eol));
            } else {
                // no description, try to find a non-empty checklist item
                List<CheckListItem> checklist = TaskFieldAdapters.CHECKLIST.get(mValues);
                if (checklist != null && checklist.size() > 0) {
                    for (CheckListItem item : checklist) {
                        String trimmedItem = item.text.trim();
                        if (!TextUtils.isEmpty(trimmedItem)) {
                            TaskFieldAdapters.TITLE.set(mValues, trimmedItem);
                            break;
                        }
                    }
                }
            }
        }

        if (!TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)) || mValues.isUpdate()) {

            if (mValues.updatesAnyKey(RECURRENCE_VALUES)) {
                mValues.ensureUpdates(RECURRENCE_VALUES);
            }

            mTaskUri = mValues.persist(activity);

            // return proper result
            result = new Intent();
            result.setData(mTaskUri);
            resultCode = Activity.RESULT_OK;
            toastId = R.string.activity_edit_task_task_saved;
        } else {
            toastId = R.string.activity_edit_task_empty_task_not_saved;
        }
    } else {
        Log.i(TAG, "nothing to save");
    }

    if (toastId != -1) {
        Toast.makeText(activity, toastId, Toast.LENGTH_SHORT).show();
    }

    if (result != null) {
        activity.setResult(resultCode, result);
    } else {
        activity.setResult(resultCode);
    }

    activity.finish();
}

From source file:com.granita.tasks.EditTaskFragment.java

/**
 * Persist the current task (if anything has been edited) and close the editor.
 *//*  w w  w . jav a  2s . com*/
public void saveAndExit() {
    // TODO: put that in a background task
    Activity activity = getActivity();

    int resultCode = Activity.RESULT_CANCELED;
    Intent result = null;
    int toastId = -1;

    //custom start
    if (mEditor != null) {
        mEditor.updateValues();
    }

    int bogusvar = 1; // set bogus variable
    toastId = R.string.disableedit; //set error message

    if (mValues.isInsert() || mValues.isUpdate())
    //if(bogusvar == 0)
    {
        if (TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues))) {
            // there is no title, try to set one from the description or check list

            String description = TaskFieldAdapters.DESCRIPTION.get(mValues);
            if (description != null) {
                // remove spaces and empty lines
                description = description.trim();
            }

            if (!TextUtils.isEmpty(description)) {
                // we have a description, use it to make up a title
                int eol = description.indexOf('\n');
                TaskFieldAdapters.TITLE.set(mValues, description.substring(0, eol));
            } else {
                // no description, try to find a non-empty checklist item
                List<CheckListItem> checklist = TaskFieldAdapters.CHECKLIST.get(mValues);
                if (checklist != null && checklist.size() > 0) {
                    for (CheckListItem item : checklist) {
                        String trimmedItem = item.text.trim();
                        if (!TextUtils.isEmpty(trimmedItem)) {
                            TaskFieldAdapters.TITLE.set(mValues, trimmedItem);
                            break;
                        }
                    }
                }
            }
        }

        if (!TextUtils.isEmpty(TaskFieldAdapters.TITLE.get(mValues)) || mValues.isUpdate()) {

            if (mValues.updatesAnyKey(RECURRENCE_VALUES)) {
                mValues.ensureUpdates(RECURRENCE_VALUES);
            }

            mTaskUri = mValues.persist(activity);

            // return proper result
            result = new Intent();
            result.setData(mTaskUri);
            resultCode = Activity.RESULT_OK;
            toastId = R.string.activity_edit_task_task_saved;
        } else {
            toastId = R.string.activity_edit_task_empty_task_not_saved;
        }
    } else {
        Log.i(TAG, "nothing to save");
    }

    if (toastId != -1) {
        Toast.makeText(activity, toastId, Toast.LENGTH_SHORT).show();
    }

    if (result != null) {
        activity.setResult(resultCode, result);
    } else {
        activity.setResult(resultCode);
    }

    activity.finish();
}