Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...