Android: app not returning correctly from intent

254
June 16, 2017, at 4:57 PM

I have an activity, that, when an ImageView is clicked, it will open a second activity for selecting and cropping an image.

This process works fine. In the second activity, when the user is satisfied with the picture, he will click DONEbutton and go back to previous activity, and the cropped image will be showed in the ImageView clicked.

The problem: the first activity is no longer showed after going back from second activity....in my real device, it goes back to starting Android menu, if I try to check the opened apps, my app is there, but even if I click on it again, it does not go to the first activity...seems like it repeats the process again for selecting and cropping image.....weird :S

Here is the relevant pieces of code I am using to move from one activity to another:

On the first activity I have:

public void onImageViewClicked(View v){
        this.clickedImageView = (ImageView) v;
        Intent intent = new Intent(this, CropImages.class);
        switch (v.getId()){
            case R.id.imgvW_1:
                startActivityForResult(intent, 1111);
                break;
            case R.id.imgvW_2:
                startActivityForResult(intent, 2222);
                break;
            case R.id.imgvW_3:
                startActivityForResult(intent, 3333);
                break;
        }
    }

And the result should come here:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){    
            if (requestCode == 1111){
                Bitmap b = (Bitmap) data.getParcelableExtra("Image");
                img_encoded_1 = Utilities.encode64Bitmap(b);
                clickedImageView.setImageBitmap(b);

            }
        }
    }

The second activity (CropImages.java) does a lot of stuff....but should end like this:

public void onDoneClicked(View v){
        // cropped Bitmap stored in cropped
        Intent returnIntent = getIntent();
        returnIntent.putExtra("Image", cropped);
        setResult(RESULT_OK, returnIntent);
        finish();
    }

Any clue why the first activity disappears and the real device just shows the welcome/main Android screen??

If that helps: the second activity also launches an intent, for selecting an image from Gallery or Camera....I do not think that matters though....

if that helps...It seems like onActivityResult on the first activity is never executed...a TOAST on that method is never showed.

Answer 1

This might be the issue, change the following in your First Activity.

Intent intent = new Intent({First Acticity name here}.this, CropImages.class);
Answer 2

Change the following code

public void onDoneClicked(View v){
    // cropped Bitmap stored in cropped
    Intent returnIntent = getIntent();
    returnIntent.putExtra("Image", cropped);
    setResult(RESULT_OK, returnIntent);
    finish();
}

To

public void onDoneClicked(View v){
    // cropped Bitmap stored in cropped
    Intent returnIntent = new Intent();  ;
    returnIntent.putExtra("Image", cropped);
    setResult(RESULT_OK, returnIntent);
    finish();
}

For your reference

https://www.javatpoint.com/android-startactivityforresult-example

Rent Charter Buses Company
READ ALSO
IndexedDB is automatically being recovered after app installation

IndexedDB is automatically being recovered after app installation

following problem: I'm running a Cordova-based hybrid app and use IndexedDB as my data storageOn iOS everything's fine, but on Android there's the problem, that on some devices this data is being restored after a new installation of the app

276
Unity 3D inside Android Studio

Unity 3D inside Android Studio

I have an Android app and it has three optionsWhat I want is when I click the first button it should open a Android VR app made in Unity which has a 360 degree video

552
Unable to destroy activity LocationTrack.stopListener()' on a null object reference

Unable to destroy activity LocationTrack.stopListener()' on a null object reference

In my android app I am getting GPS location in splash screen and storing in a sessionIn my fragment based on location I am fetching the data from database, if user wants to change the location on click action bar its going to SelectLocationActivity there is button...

412
How to set up vpn connection with username and password using Android

How to set up vpn connection with username and password using Android

There are many examples of connecting by entering the server address and port numberToyVpnClient is one of them

441