Android onActivityResult NEVER called

27,331

Solution 1

The solution is to call a transparent activity over top of the main activity. This transparent activity is in front of the tabhost and will have normal lifecycle functions.

This transparent activity calls the gallery intent onCreate(), it gets everything returned like normal in its onActivityResult and you will be able to pass the information returned back to the rest of the app like normal. finish() is inside of the onActivityResult method, so the user never even notices that a transparent activity was called.

Update copied from from comments:

Activity A calls Activity B via normal intent. Activity B has no xml and runs onCreate like this

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.dialogpopper); 

    Intent intent = new Intent(Intent.ACTION_PICK); 
    intent.setType("image/*"); startActivityForResult(intent, 0);

}//end onCreate 

and when Activity C is finished it calls the onActivityResult of Activity B

Solution 2

Try to call the startActivityForResult using the context of the tabgroup activity containing your current activity and then listen in the tabgroup activity.

Use this to get the tabGroupActivity:

TabGroupActivity parentActivity = (TabGroupActivity)getParent();

And then call startActivityForResult from it:

parentActivity.startActivityForResult(...);

Finally , put an onActivityResult listener in the tabGroupActivity:

protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
    }

Solution 3

Judging from the many questions like this one, there are many reasons why a called activity may not trigger the caller's onActivityResult() method.

One reason I found, was when I called startActivityForResult(intent, requestCode), with a requestCode value of less than 0. My application did not need a requestCode and the Android documentation said using < 0 would not send a requestCode.

But the Android docs did not mention the consequence of a requestCode < 0. The consequence is that it prevents the caller's onActivityResult() method from ever being invoked! Ouch!

Therefore, even if your app does not need a requestCode, you many still want to use one with a value >= 0.

That's what I learned today:-)

Solution 4

You just have to remove android:noHistory="true" this form your manifest file.

Solution 5

Use the constant values for the Result codes:

Activity.RESULT_OK and Activity.RESULT_CANCELED

You'll see that the value for cancelled is actually 0. So in your code you are checking to see if the activity was cancelled.

change your code to

if (resultCode == Activity.RESULT_OK) {
...
}

Additionally change your Intent action to be:

intent.setAction(Intent.ACTION_PICK);

If you do this, you can just call

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, 0); 

instead of creating the chooser. It will automatically pick the activities associated with that intent and mimetype and display them to you

Share:
27,331
CQM
Author by

CQM

Updated on January 31, 2020

Comments

  • CQM
    CQM over 4 years

    my onActivityResult method is never called. am using android 2.2

    I am using a Tabhost, where TabHosts contain TabGroups which contain individual Activities.

    One of my individual activity runs the following intent

     Intent intent = new Intent(); 
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), 0);
    

    this loads my gallery apps, I use the default android gallery to select one image and when I return my onActivityResult is not called my activity.

    It looks like this - and I put a breakpoint at if(resultCode == 0) , so right now, the logic of my onActivityResult should not matter

     public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 0) {
            if (requestCode == 0) {
                Uri selectedImageUri = data.getData();
    
                //OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();
    
                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);
    
                //DEBUG PURPOSE - you can delete this if you want
                if(selectedImagePath!=null)
                    System.out.println(selectedImagePath);
                else System.out.println("selectedImagePath is null");
                if(filemanagerstring!=null)
                    System.out.println(filemanagerstring);
                else System.out.println("filemanagerstring is null");
    
                //NOW WE HAVE OUR WANTED STRING
                if(selectedImagePath!=null)
                    System.out.println("selectedImagePath is the right one for you!");
                else
                    System.out.println("filemanagerstring is the right one for you!");
            }
        }
    }
    

    Lifecycle functions are often called out of order and intermittently for Activities within a tabhost/tabgroup, so I checked to see what lifecycle functions ARE being called after the gallery closes (this happens as soon as I select an image from the android gallery)

    The only one being called is the onResume() in my TabHost activity. So I tried putting the exact same onActivityResult() method in my TabHost class AS WELL AS the TabActivity class. With a breakpoint in the same location at the beginning of method.

    Neither of these classes are called.

    I'm drawing a blank now, how can I get the result from the gallery app in my app if none of the built in receiving methods will respond to it.

    Since I know that my main TabHost gets the onResume() called, I tried added Intent graphics = getIntent(); to see if it would receive data from the gallery selection, it does not, so I don't see how I can do the logic in the onResume() method either.

    Solutions welcome! :)