Android:如何使活动将结果返回给调用它的活动? [英] Android: how to make an activity return results to the activity which calls it?

查看:12
本文介绍了Android:如何使活动将结果返回给调用它的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Location 活动,可以从许多活动中调用,例如 Sign upOrder.在 Location 活动中,用户输入他的位置,因此活动 Location 会将这个新位置返回给调用它的那个活动.

I have a Location activity that can be called from many activities, such as Sign up and Order. In the Location activity the user enters his location, so the activity Location will return this new location to that activity which called it.

因此,当Sign up 活动调用Location 活动时,它必须将数据返回给Sign up 活动.另一次 Order 活动会做同样的事情.

So when the Sign up activity calls the Location activity, it has to return the data to the Sign up activity. Another time the Order activity will do the same thing.

我知道你会告诉我应该发布代码,但我不是要你给我代码;我只想要一些提示、链接或好的话题.

I know you will tell me that I should post the code, but I am not asking you to give me the code; I just want some tips, links or good threads.

推荐答案

为了启动一个应该将结果返回给调用 Activity 的 Activity,您应该执行以下操作.您应该传递如下所示的请求代码,以便确定您从启动的活动中获得了结果.

In order to start an activity which should return result to the calling activity, you should do something like below. You should pass the requestcode as shown below in order to identify that you got the result from the activity you started.

startActivityForResult(new Intent("YourFullyQualifiedClassName"),requestCode);

在活动中你可以使用setData()返回结果.

In the activity you can make use of setData() to return result.

Intent data = new Intent();
String text = "Result to be returned...."
//---set the data to pass back---
data.setData(Uri.parse(text));
setResult(RESULT_OK, data);
//---close the activity---
finish();

那么在第一个活动中,您再次在 onActivityResult() 中编写以下代码

So then again in the first activity you write the below code in onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) {
            String returnedResult = data.getData().toString();
            // OR
            // String returnedResult = data.getDataString();
        }
    }
}

根据您的评论进行如果你想返回三个字符串,那么通过使用带有意图的键/值对而不是使用 Uri 来遵循这个.

EDIT based on your comment: If you want to return three strings, then follow this by making use of key/value pairs with intent instead of using Uri.

Intent data = new Intent();
data.putExtra("streetkey","streetname");
data.putExtra("citykey","cityname");
data.putExtra("homekey","homename");
setResult(RESULT_OK,data);
finish();

onActivityResult 中获取它们,如下所示:

Get them in onActivityResult like below:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) {
            String street = data.getStringExtra("streetkey");
            String city = data.getStringExtra("citykey");
            String home = data.getStringExtra("homekey");
        }
    }
}

这篇关于Android:如何使活动将结果返回给调用它的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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