博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【慢慢学Android】:1.Activity之间的转换以及数据的传递(Intent)
阅读量:4591 次
发布时间:2019-06-09

本文共 3585 字,大约阅读时间需要 11 分钟。

 当作自己的笔记,参考引用了一些大神文章:

 

   Intent简介:                                                                               

      在一个Android应用中,主要由四种组件组成(四种组件分别为:Activity、Broadcast、Service、ContentProvider),而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。在这些组件之间的通讯中,主要是由Intent协助完成的。

      Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

   Intent来实现Activity之间的跳转:                                           

   1.无数据的简单跳转   

通常在button的setOnClickListener中,通过构造一个新的intent实例,然后通过startActivity(Intent实例)来实现:

open.setOnClickListener(new OnClickListener(){			@Override			public void onClick(View v) {				// TODO Auto-generated method stub				Intent intent = new Intent(MainActivity.this, FileBrowser.class);				startActivity(intent);			}		});

  为什么需要指定两个活动呢?因为在Android中有一个活动栈,这样的构造方式才能确保正确的将前一个活动压入栈中,才能在触发返回键的时候活动能够正确出栈。

   2.所有Activity都必须在AndroidManifest.xml中声明:   

  

   3.Intent常用的构造函数:   

A.Intent(String action) 指定action类型的构造函数

B.Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

C.Intent(Context packageContext, Class<?> cls) 传入组件的构造函数(示例一中提到的Activity之间的转换)

D.Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

Intent(String action, Uri uri)  的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

Intent intent = new Intent(Intent.ACTION_EDIT, null);    startActivity(intent);

  该示例所使用的是构造函数B,Intent.ACTION_EDIT表示的是一个Action,执行此代码的时候,程序就会在AndroidManifest.xml中寻找相应的URI下所属的Activity;

<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应为多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity,如下图:

 如果是用示例代码一那种方式进行发送则不会有这种情况。

   Activity之间数据的传递:                                                         

首先需要使用到的是Bundle

String path = file.getAbsolutePath();			String name = file.getName();			Intent intent = new Intent(FileBrowser.this,MainActivity.class);			Bundle bundle=new Bundle();		    bundle.putString("path",path );		    bundle.putString("name", name);		    intent.putExtras(bundle);       		    startActivity(intent);

首先,示例中创建了一个Intent的实例,用于转换Activity,同时,从该Activity中的变量获取了需要传递的数据;

其次是bundle.putXXX()方法的调用,向bundle中填充数据

void ( key, int value)
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key, int[] value)
Inserts an int array value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key, long value)
Inserts a long value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key, boolean value)
Inserts a Boolean value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key, boolean[] value)
Inserts a boolean array value into the mapping of this Bundle, replacing any existing value for the given key.
void ( map)
Inserts all mappings from the given Bundle into this Bundle.
void ( key,  value)
Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key,  value)
Inserts a String array value into the mapping of this Bundle, replacing any existing value for the given key.
void ( key, <> value)
Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

然后调用intent.putExtras(bundle),将bundle绑定在intent上,最后startActivity();

   数据的接收:   

Bundle bd = intent.getExtras();        String str_path = bd.getString("path");        String str_name = bd.getString("name");        text = (TextView)findViewById(R.id.tv_address);        name = (TextView)findViewById(R.id.tv_name);        text.setText(str_path);        name.setText(str_name);

 

转载于:https://www.cnblogs.com/VortexPiggy/archive/2012/05/25/2509465.html

你可能感兴趣的文章
浏览器中开发人员工具快速找到dom元素绑定那些JS事件
查看>>
js数据结构与算法——集合
查看>>
程序员技术练级攻略(转载)
查看>>
Servlet入门
查看>>
【JQuery】jQuery(document).ready(function($) { });的几种表示方法及load和ready的区别
查看>>
单目运算符-双目运算符-三目运算符
查看>>
canvas图像以及剪切
查看>>
cookie ,session Storage, local storage
查看>>
finereport9.0破解版|finereport10.0破解并发数|finereport授权注册|FineBI5.0破解lic
查看>>
用10张图来看机器学习Machine learning in 10 pictures
查看>>
使用node.js定义一个web服务器
查看>>
任务16 被动信息收集
查看>>
1282: 排列计数 perm
查看>>
牛客小白月赛15 C 表单 ( map 使用)
查看>>
oracle中的索引
查看>>
STM8S——Analog/digital converter (ADC)
查看>>
LeetCode-211 Add and Search Word - Data structure design
查看>>
jquery each遍历节点使用
查看>>
sql笔记 获取指定数据库下的所有表
查看>>
第一个定时脚本--nginx日志的切割
查看>>