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

相关文章

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.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现