Android 2.2 API Demos -- 通过调用子Activity返回值

2024-03-25 02:18

本文主要是介绍Android 2.2 API Demos -- 通过调用子Activity返回值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们使用Intent可以将数据从一个Activity传递到下一个Activity,同样,在Android中我们可以将数据从一个Activity返回给前一个Activity。

参考API Demo示例:

1. 定义父Activity,ReceiveResult.java。在这个Activity中我们通过startActivityForResult(intent, GET_CODE)启动子Activity。
Java代码

package com.example.android.apis.app;import java.util.Map;import com.example.android.apis.R;import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;import java.util.Map;public class ReceiveResult extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// Be sure to call the super class.super.onCreate(savedInstanceState);// See assets/res/any/layout/hello_world.xml for this// view layout definition, which is being set here as// the content of our screen.setContentView(R.layout.receive_result);// Retrieve the TextView widget that will display results.mResults = (TextView)findViewById(R.id.results);// This allows us to later extend the text buffer.mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);// Watch for button clicks.Button getButton = (Button)findViewById(R.id.get);getButton.setOnClickListener(mGetListener);}@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent data) {// You can use the requestCode to select between multiple child// activities you may have started.  Here there is only one thing// we launch.if (requestCode == GET_CODE) {// We will be adding to our text.Editable text = (Editable)mResults.getText();// This is a standard resultCode that is sent back if the// activity doesn't supply an explicit result.  It will also// be returned if the activity failed to launch.if (resultCode == RESULT_CANCELED) {text.append("(cancelled)");// Our protocol with the sending activity is that it will send// text in 'data' as its result.} else {text.append("(okay ");text.append(Integer.toString(resultCode));text.append(") ");if (data != null) {text.append(data.getAction());}}text.append("\n");}}// Definition of the one requestCode we use for receiving resuls.static final private int GET_CODE = 0;private OnClickListener mGetListener = new OnClickListener() {public void onClick(View v) {// Start the activity whose result we want to retrieve.  The// result will come back with request code GET_CODE.Intent intent = new Intent(ReceiveResult.this, SendResult.class);startActivityForResult(intent, GET_CODE);}};private TextView mResults;
}


我们需要为startActivityForResult传递一个Intent和一个请求码。

Intent决定启动哪个Activity。请求码是对子Activity标记的唯一ID。假如从一个父Activity中可能启动的子Activity有多个,那么通过请求码我们就可以知道是从哪个子Activity返回的。

 

2. 定义子Activity,SendResult.java。在这个Activity中我们通过setResult(RESULT_OK, (new Intent()).setAction("Corky!"))将结果返回给父Activity。
Java代码

package com.example.android.apis.app;import com.example.android.apis.R;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class SendResult extends Activity
{@Overrideprotected void onCreate(Bundle savedInstanceState){// Be sure to call the super class.super.onCreate(savedInstanceState);// See assets/res/any/layout/hello_world.xml for this// view layout definition, which is being set here as// the content of our screen.setContentView(R.layout.send_result);// Watch for button clicks.Button button = (Button)findViewById(R.id.corky);button.setOnClickListener(mCorkyListener);button = (Button)findViewById(R.id.violet);button.setOnClickListener(mVioletListener);}private OnClickListener mCorkyListener = new OnClickListener(){public void onClick(View v){// To send a result, simply call setResult() before your// activity is finished.setResult(RESULT_OK, (new Intent()).setAction("Corky!"));finish();}};private OnClickListener mVioletListener = new OnClickListener(){public void onClick(View v){// To send a result, simply call setResult() before your// activity is finished.setResult(RESULT_OK, (new Intent()).setAction("Violet!"));finish();}};
}


在调用finish()之前,要先调用setResult方法,将结果码和数据返回给父Activity。在android.app.Activity中定义两个标准结果码,RESULT_OK和RESULT_CANCELED。

如果子Activity启动失败或者没有显式的在setResult()中设置结果码,子Activity会默认返回RESULT_CANCELED。

3. 在父Activity中,我们重写onActivityResult(int requestCode, int resultCode,  Intent data)方法,接收子Activity返回的数据。参考1中的代码。

请求码(requestCode):通过它我们可以对多个子Activity进行处理。

结果码(resultCode):通过它我们可以判断子Activity的处理结果,对不同的结果码进行相应的操作。

这篇关于Android 2.2 API Demos -- 通过调用子Activity返回值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/843552

相关文章

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

使用Go调用第三方API的方法详解

《使用Go调用第三方API的方法详解》在现代应用开发中,调用第三方API是非常常见的场景,比如获取天气预报、翻译文本、发送短信等,Go作为一门高效并发的编程语言,拥有强大的标准库和丰富的第三方库,可以... 目录引言一、准备工作二、案例1:调用天气查询 API1. 注册并获取 API Key2. 代码实现3

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

Python如何调用另一个类的方法和属性

《Python如何调用另一个类的方法和属性》在Python面向对象编程中,类与类之间的交互是非常常见的场景,本文将详细介绍在Python中一个类如何调用另一个类的方法和属性,大家可以根据需要进行选择... 目录一、前言二、基本调用方式通过实例化调用通过类继承调用三、高级调用方式通过组合方式调用通过类方法/静