Android的onActivityResult始终为0 [英] Android onActivityResult is always 0

查看:550
本文介绍了Android的onActivityResult始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经杀了我两天了。我有一个主要活动A中调用第二个活动B. b活动只需presents一个列表视图的用户。当我preSS的项目在列表视图我想了几个字符串传递回主活动A和Activiy B就完成。

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.

现在的问题是,我总是得到0的结果code和数据包是空。我真的不明白为什么会这样。

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.

下面是我的code。

开始b活动的结果;

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

这将启动第二个活动的罚款。 b活动填充一个列表视图,当我点击一个项目,我想一些数据发送回调用活性方法。

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.

在目前的任何文本,所以我用下面的b活动;

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;

                } 
            } 
        }

我可以看到,请求code是正确返回,但结果code是始终是0,数据总是空。

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

我通过调试运行,并且是的setResult做它的工作和包确实有我传递的数据,但它失去了沿途的一些点。

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.

任何帮助将真正成为AP preciated。

Any help would truly be appreciated.

谢谢, 院长

推荐答案

在你的名单活动,onItemClickListener请尝试以下操作与替换的setResult行:

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

这篇关于Android的onActivityResult始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆