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

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.