简体   繁体   中英

How to make an intent and launch an activity in onActivityResult()?

I am trying to launch an activity after a user has selected a photo. I was trying to do this:

        uploadImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                selectImageIntent.setType("image/*");

                startActivityForResult(selectImageIntent, 1);

                Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
                goToActivityIntent.putExtra("email", email);
                goToActivityIntent.putExtra("donorEmail", donorEmail);
                goToActivityIntent.putExtra("orderId", orderId);
                goToActivityIntent.putExtra("uriString", uriString);

                view.getContext().startActivity(goToActivityIntent);
            }
        });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && data != null) {
            uriString = data.getData().toString();
        }
    }

But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.

I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().

I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().

If you are in Activity then you can just use this as Context

 Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);

If you are in a Fragment then you can obtain Context by calling getContext()

Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);

And use that code inside onActivityResult() as you were trying to.

set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:

if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {

  Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
            goToActivityIntent.putExtra("email", email);
            goToActivityIntent.putExtra("donorEmail", donorEmail);
            goToActivityIntent.putExtra("orderId", orderId);
            goToActivityIntent.putExtra("uriString", uriString);

            view.getContext().startActivity(goToActivityIntent);
        if (data == null) {
            //Display an error
            println("error accuered at onActivityResult ")
            return
        }

Have you tried this simpler one:

uploadImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                selectImageIntent.setType("image/*");

                startActivityForResult(selectImageIntent, 1);
            }
        });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && data != null) {
            uriString = data.getData().toString();
             Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
                goToActivityIntent.putExtra("email", email);
                goToActivityIntent.putExtra("donorEmail", donorEmail);
                goToActivityIntent.putExtra("orderId", orderId);
                goToActivityIntent.putExtra("uriString", uriString);

                startActivity(goToActivityIntent);
        }
    }

Just call

startActivity(goToActivityIntent);

to call the activity.

This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM