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

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

问题描述

我有一个位置的活动,这个活动可以从许多活动被称为如注册和活动位置用户输入自己的位置,这样的活动位置将返回这个新的位置,该活动而调用它。

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

我的意思是有时`报名`活动调用`location`活动,这样的位置活动有将数据返回到注册活动

I mean sometimes the `sign up `activity calls the `location` activity so the location activity has to return the data to the sign up activity

还有一次在活动将做同样的事情。

Another time the order activity will do the same thing.

我希望我可以让这个问题可以理解的,因为我不好英语

I hope I could make the question understandable because I am bad on English

我知道你会告诉我说,你应该把code,我不要求你给我的codeI知道这是不允许的,我只是想给我一些提示,链接还是不错的线程

I know you will tell me that you should put code, I am not asking you to give me the code i know that is not allowed, i just want to give me some tips , links or good threads

推荐答案

为了启动,应返回结果给调用活动的一个活动,你应该做的事情如下图所示。你应该把这个请求code为,以确定你得到了你开始活动的结果如下图所示。

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();

于是又在第一个活动,你写如下的code在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();
        }
    }
}

编辑根据您的评论: 如果你想返回三个字符串,然后通过使用键/值对的意图,而不是使用乌里遵循这一点。

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 象下面这样:

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天全站免登陆