Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onActivityResult is always 0

This has been killing me for two days now. I have a main Activity A which calls a second Activity B. Activity B simply presents the user with a listview. When I press an item on the list view I want a couple of strings to be passed back to the main Activity A and Activiy B will finish.

The problem is I always get a resultcode of 0 and the data bundle is null. I really don't understand why this is happening.

Here is my code.

Start Activity B for result:

Test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(recipeActivity.this, BrowseLoadRecipes.class);
            startActivityForResult(i, RECIPE_CHOOSER);
    }  
    });

This starts the second Activity fine. Activity B populates a listview and when I click an item I'm trying to send some data back to the calling Activity A.

Any text at the moment, so I used the following in Activity B:

     lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", "Please work... pleeeeaasee");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
     }
     });

In the calling activity I have the following listening for the return as follows:

protected void onActivityResult(int requestCode, int resultCode, 
        Intent data) { 
            switch(requestCode) { 
            //TODO
            case RECIPE_CHOOSER:
                Toast.makeText(getApplicationContext(), "In recipe return", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "resultCode is " + String.valueOf(resultCode), Toast.LENGTH_SHORT).show();
                if (resultCode == RESULT_OK) {
                Bundle b = getIntent().getExtras();

                Toast.makeText(getApplicationContext(), "Returned " + b.getString("TEXT"), Toast.LENGTH_LONG).show();
                }
                if (resultCode == RESULT_CANCELED) {

                    }
                break;

                } 
            } 
        }

I can see that the request code is correctly returned, but the resultcode is always a 0 and the data is always a null.

I've ran through the debug and the setResult is doing its job and the bundle does indeed have the data I'm passing, but it's lost at some point along the way.

Is there something in the manifest I'm missing or something. It's killed my progress on this project so far.

like image 536
Dean Avatar asked Dec 22 '10 14:12

Dean


People also ask

What is result code in onActivityResult?

onActivityResult - resultCode is always 0.

How does onActivityResult work on Android?

class); startActivityForResult(i, 100); 2.In your secondActivity class write following code for onClick Event For ex: In secondActivity if you want to send back data: Intent intent= new Intent(); intent. putExtra("result",result); setResult(RESULT_OK,intent); finish();

Can startActivityForResult still be used?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


2 Answers

In your list activity, onItemClickListener try the following replacing the setResult lines with:

if (getParent() == null) {
    setResult(Activity.RESULT_OK, data);
}
else {
    getParent().setResult(Activity.RESULT_OK, data);
}

I'm wondering whether there is a parent activity which is what you need to bind the data to and set the result on....

like image 68
David Brown Avatar answered Nov 07 '22 04:11

David Brown


Concerning your returned data.

You do:

Bundle b = getIntent().getExtras();

but "getIntent()" returns the Intent that started THIS activity. If you want the returned data from the Activity you started for result, just take the data which is passed to

protected void onActivityResult(int requestCode, int resultCode, Intent data)
like image 21
metter Avatar answered Nov 07 '22 04:11

metter