startActivityForResult 的问题

发布于 2024-12-03 22:10:59 字数 856 浏览 1 评论 0原文

我有一个活动启动另一个具有 EditText 视图的活动。当用户单击“后退”按钮时,我希望返回此编辑文本视图中的字符串,但我无法让它工作。我尝试实现一个按钮(测试目的)并运行我的代码,然后它就可以工作了。

在启动另一个活动的活动中:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (REQUEST_CODE_COMMENT == requestCode) {
            if (RESULT_OK == resultCode) {
                Toast.makeText(this, data.getDataString(), Toast.LENGTH_LONG).show();
            }
        }
    }

具有编辑文本视图的活动(我希望它能够工作):

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent();
    intent.setData(Uri.parse("hfldskajfkj" + commentEditText.getText().toString()));
    setResult(RESULT_OK, intent);   
}

但是当我记录它时,结果代码是 0 (RESULT_CANCELLED)。

I have one activity with that launches another activity that has a EditText-view. When the user clicks the "Back" button I want the string in this edit text view to be returned but I can't get it to work. I tried to implement a button (test purpose) and running my code and then it works.

In the activity that launches the other one:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (REQUEST_CODE_COMMENT == requestCode) {
            if (RESULT_OK == resultCode) {
                Toast.makeText(this, data.getDataString(), Toast.LENGTH_LONG).show();
            }
        }
    }

The activity with the edit text view (I want this to work):

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent();
    intent.setData(Uri.parse("hfldskajfkj" + commentEditText.getText().toString()));
    setResult(RESULT_OK, intent);   
}

But the resultCode is 0 (RESULT_CANCELLED) when i log it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-12-10 22:10:59
public void onBackPressed() {
        finish();
}

这是 onBackPressed() 的默认实现(也在 文档)。这意味着您的活动在调用其他代码之前已完成(使用默认代码 RESULT_CANCELLED)。删除 super.onBackPressed() 并在底部添加 finish() 应该可以解决此问题。

public void onBackPressed() {
        finish();
}

Thats the default implementation of onBackPressed() (also mentioned in the documentation). This means your activity gets finished (with the default code RESULT_CANCELLED) before your other code gets invoked. Removing super.onBackPressed() and adding finish() on the bottom should fix this.

清欢 2024-12-10 22:10:59

问题是这样的: API 说:

public void onBackPressed () 自:API 级别 5

当活动检测到用户按下后退键时调用。默认
实现只是完成当前活动,但您可以覆盖它来执行
无论你想要什么。

当您调用 super.onBackPressed() 时,对特定活动的所有引用都将被清除。因此,未添加特定数据。尝试在按钮的 OnClickListener 中插入相同的代码。那应该有效

The problem is this: The API says:

public void onBackPressed () Since: API Level 5

Called when the activity has detected the user's press of the back key. The default
implementation simply finishes the current activity, but you can override this to do
whatever you want.

And when you call super.onBackPressed() all the references to the particular activity are cleared. Hence the particular data is not added. Try the same code inserted in OnClickListener of a button. That should work

天赋异禀 2024-12-10 22:10:59

Android UI 指南中的某个地方说你不应该有明确的“保存”按钮。在这种情况下,答案很简单:不必费心检查结果代码是否为 RESULT_OK,无论如何,始终使用 EditText 小部件的最后一个值。

Somewhere in the Android UI guidelines it says you should not have explicit “save” buttons. In this case, the answer is simple: don’t bother checking if the result code is RESULT_OK, always use the last value of the EditText widget regardless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文