Activity间中使用Intent传值
主页面用来输入一个值传入第二个页面显示,关闭第二个页面返回一个值
主页布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <TextView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭第二个页面后返回值" /> <EditText
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view"
android:text="" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:text="转第二页" />
</RelativeLayout>
MainActivity,主要通过startActivityForResult来传递请求码,返回数据的时候,第二个页面调用Activity.setResult()方法设置返回Intent以及返回码,需要重写源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中会判断请求码和响应码
package com.example.android01; import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private final static String TAG="MainActivity";
private final static int REQUEST_CODE = 1;
private final static String CONTENT = "content";
private EditText txtEditText = null;
private Button btnButton = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEditText = (EditText) this.findViewById(R.id.txt);
if (savedInstanceState != null && savedInstanceState.containsKey(CONTENT)) {
txtEditText.setText(savedInstanceState.getString(CONTENT) + "[2]");
}
btnButton = (Button) this.findViewById(R.id.btn);
btnButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String str=txtEditText.getText().toString();
intent.putExtra("fromMain", str);
// startActivity(intent);
// 启动需要监听返回值的Activity,并设置请求码:requestCode
startActivityForResult(intent, REQUEST_CODE);
}
});
} /*
* (non-Javadoc)
*
* @see android.app.Activity#onActivityResult(int, int,
* android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 当otherActivity中返回数据的时候,会响应此方法
// requestCode和resultCode必须与请求startActivityForResult()和返回setResult()的时候传入的值一致。
if (requestCode == REQUEST_CODE && resultCode == SecondActivity.RESULT_CODE) {
Bundle bundle=data.getExtras();
String strResult = bundle.getString("result");
Log.i(TAG,"onActivityResult: "+ strResult);
Toast.makeText(MainActivity.this, strResult, Toast.LENGTH_LONG).show();
}
} /*
* (non-Javadoc) 保存Activity页面状态在onStop之前执行
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
String contentString = txtEditText.getText().toString();
outState.putString(CONTENT, contentString);
} /*
* (non-Javadoc)
*
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} }
每二个Activity
/**
*
*/
package com.example.android01; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; /**
* @author Administrator 2014-3-12 下午10:21:17
*/
public class SecondActivity extends Activity { private final static String TAG="MainActivity";
public final static int RESULT_CODE=1;
private Button btnBack = null;
private EditText txtContent = null;
private TextView viewContent = null; /*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity); txtContent = (EditText) this.findViewById(R.id.txtContent);
viewContent = (TextView) this.findViewById(R.id.viewContent);
btnBack = (Button) this.findViewById(R.id.btnBack); Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String strContentString = bundle.getString("fromMain");
Log.i(TAG,"SecondActivity: "+ strContentString);
viewContent.setText(strContentString);
btnBack.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.putExtra("result", txtContent.getText().toString());
setResult(RESULT_CODE, intent);// 设置resultCode,onActivityResult()中能获取到
finish();
}
});
} }
注意请求码与返回码的对应判断,因为可能有多个请求码与返回码。
Activity间中使用Intent传值的更多相关文章
- 在Activity之间使用Intent传值和Bundle传值的区别和方式
两者本质上没有任何区别.Bundle只是一个信息的载体 将内部的内容以键值对组织 Intent负责Activity之间的交互 自己是带有一个Bundle的Intent.putExtras(Bundle ...
- Activity以singleTask模式启动,intent传值的解决办法
转载请注明出处,谢谢http://blog.csdn.net/harryweasley/article/details/46557827 因为项目中,有一个消息推送的功能,每次推送一个消息,就会开启F ...
- Android中Intent传值与Bundle传值的区别详解
Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面 这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...
- 安卓笔记--intent传值不更新问题
今天在学习安卓的过程中,遇到一个问题,就是用intent进行多次传值的话, 他永远是第一次的值 后来发现,intent接收数据被写到了onCreat();方法中,这时候finish();到上一个Act ...
- Intent传值的学习
今天学习了Intent传值的过程,有点安卓编程经验的都知道,Intent可以实现页面的跳转,可以从一个activity跳转到另一个activity,这个名义上说是界面跳转,其实这句话现在觉得说的很不严 ...
- Android intent 传值不更新的原因和解决办法
当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候.如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题.也就是说每次 int ...
- Android学习之Activity跳转与传值
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一 Intent intent = new Intent(A.this, ...
- Activity: launchMode 和 Intent.FLAG_ACTIVITY_CLEAR_TOP
Activity 的 launchMode: 1. standard: 标准模式 这种启动模式为标准模式,也是默认模式.每当我们启动一个Activity,系统就会相应的创建一个实例,不管这个实例是否已 ...
- Activity之间使用intent传递大量数据带来问题总结
转载:大飞 http://blog.csdn.net/rflyee/article/details/47441405 Activity之间使用Parcel传递大量数据产生的问题. Activity ...
随机推荐
- 《Python标准库》 目录
目录 译者序序前言第1章 文本1.1 string—文本常量和模板1.1.1 函数1.1.2 模板1.1.3 高级模板1.2 textwrap—格式化文本段落1.2.1 示例数据1.2.2 填充段落1 ...
- Android学习笔记之消息机制
Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程. 1.为什么要使用Handler? Android规定访问UI只 ...
- GP调用arctoolbox 以Clip为例
GP的功能非常强大,也是GIS建模的一个很重要的工具.在Arcengine中,实现Clip功能很多种方法,可以用IBasicGeoprocessor的clip方法,但是GP无疑是最简单的. publi ...
- 【转】C#Winform程序如何发布并自动升级(图解)
有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...
- HTML5 history API实践
一.history API知识点总结 在HTML4中,我们已经可以使用window.history对象来控制历史记录的跳转,可以使用的方法包括: history.forward();//在历史记录中前 ...
- html表格相关
<html> <head> <style type="text/css"> thead {color:green} tbody {color:b ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- Node.js使用fs.renameSync报cross-device link not permitted错误
在Node.js中,我们可以使用formidable模块来轻松地实现文件上传功能,代码如下: var Q = require('q'); var util = require('util'); var ...
- js中setTimeout()的使用bug
今天用setTimeout()时,遇到一个奇怪的现象,通过多方面的查询,最终解决了问题,这是setTimeout()设计的时候存在的一点点bug. 代码的作用主要是在三秒后自动关闭本浏览器窗口: 代码 ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...