Android onActivityResult is always 0

自作多情 提交于 2019-11-26 20:54:15

问题


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.


回答1:


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....




回答2:


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)



回答3:


Your code is perfectly working........

in u Activity B
use
  setResult(0, mIntent);insted of setResult(RESULT_OK, mIntent);
in your Activity A:
use
case 0: insted case RECIPE_CHOOSER: and 
use System.out.println(b.getString("TEXT"));

You will get output as

Please work... pleeeeaasee



来源:https://stackoverflow.com/questions/4510280/android-onactivityresult-is-always-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!