Android中的putExtra() [英] putExtra() in android

查看:107
本文介绍了Android中的putExtra()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从最基本的角度了解putExtra的用法

i want to know the usage of putExtra from very basic level

推荐答案

如果要向意图中添加信息,则可以使用此方法.此信息表示为元组(键,值).意图的附加值中可以包含许多值类型(例如,int,int [],Bundle,Parcelable等).对于每种方法,都有一个对应的读取"方法,该方法用于从意图中获取信息.

If you want add information to your intent you can use this method. This information is represented as tuple (key, value). There are the number of value types that can be included into the extras of intent (for instance, int, int[], Bundle, Parcelable, and so on). For each this method there is a corresponding "read" method that is used to get the information from the intent.

因此,这是一个如何使用此示例.想象一下,您想从活动A显式调用活动B并将其传递给它一个整数数组:

So here is a possible example how to use this. Imagine that you want explicitly call the activity B from activity A and pass to it an array of integers:

int intArray[] = {1,2,3,4};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);

要读取活动B中的信息(在onCreate()方法中),您应该使用以下代码:

To read the information in activity B (in onCreate() method) you should use the following code:

Bundle extras = getIntent().getExtras();
int[] arrayInB = extras.getIntArray("my_array");

这篇关于Android中的putExtra()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆