在Android的是什么包是什么意思? [英] What does bundle mean in Android?

查看:289
本文介绍了在Android的是什么包是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid应用程序的开发,并且无法理解是什么捆绑实际上为我们做。

任何人能解释一下吗?

解决方案
  

我是新的Andr​​oid应用程序开发和无法理解   不包实际上为我们做什么..任何人能解释这对我来说。谢谢   提前。

在我自己的话,你可以想象它像一个 地图 ,用于存储基本数据类型对象作为夫妇的 键 - 值

捆绑最常用于通过各种 将数据传递活动 。提供的 putType() 的getType() 方法存储和从中检索数据

还有 捆绑 的onCreate() 当要保存数据时设备取向被改变(在这种情况下,活性被破坏并产生再次非零参数作为束)的活动的生命周期方法都可以使用。

更多关于 捆绑 在它的方法,你可以阅读的 在developer.android.com 参考,你应该开始,然后做一些演示应用程序来获得经验

更新:

示范使用的例子:

通过活动传递基本数据类型:

 意向书我=新的意图(ActivityContext,TargetActivity.class);
捆绑数据映射=新包();
dataMap.putString(钥匙,价值);
dataMap.putInt(钥匙,1);
i.putExtras(数据映射);
startActivity(ⅰ);
 

通过活动传递值的列表:

 捆绑数据映射=新包();
ArrayList的<字符串> S =新的ArrayList<字符串>();
s.add(你好);
dataMap.putStringArrayList(钥匙,S); //还整数和的CharSequence
i.putExtras(数据映射);
startActivity(ⅰ);
 

通过活动传递序列化对象:

 公共类Foo实现Serializable {

   私有静态最后长的serialVersionUID = 1L;

   私人的ArrayList< FooObject> FOOS;

   公共美孚(ArrayList中< FooObject> FOOS){
      this.foos = FOOS;
   }

   公众的ArrayList< FooObject> getFoos(){
      返回this.foos;
   }
}


公共类FooObject实现Serializable {

   私有静态最后长的serialVersionUID = 1L;

   私人诠释身份证;

   公共FooObject(INT ID){
      this.id = ID;
   }

   公众诠释的getId(){
      返回ID;
   }

   公共无效SETID(INT ID){
      this.id = ID;
   }
}
 

然后:

 捆绑数据映射=新包();
ArrayList的< FooObject> FOOS =新的ArrayList< FooObject>();
foos.add(新FooObject(1));
dataMap.putSerializable(钥匙,新富(FOOS));
 


通Parcelable通过活动对象:

还有更多code所以这里是文章如何做到这一点:

  • <一个href="http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/"相对=nofollow> 包裹数据使用Parcelable活动之间传递 班


如何检索目标的活动数据:

有一个神奇的方法: getIntent() 返回意向(如果有任何的数据也与扩展数据)开始活动,从有方法被调用。

所以:

 捆绑dataFromIntent = getIntent()getExtras()。
如果(dataFromIntent!= NULL){
   字符串stringValue的= dataFromIntent.getString(钥匙);
   INT的intValue = dataFromIntent.getInt(钥匙);
   美孚fooObject =(富)dataFromIntent.getSerializable(钥匙);
   所以我们需要转换为相应对象// getSerializble返回序列化。
   ArrayList的&LT;字符串&GT;字符串数组= dataFromIntent.getStringArrayList(钥匙);
}
 


捆绑的用法为的onCreate()方法的参数:

正在将数据存储在 的onSaveInstanceState() 如下方法:

  @覆盖
公共无效的onSaveInstanceState(包图){
   map.putString(钥匙,价值);
   map.putInt(钥匙,1);
}
 

而在 的onCreate() 方法(在本例中是 捆绑 作为参数不是null)如下:

  @覆盖
公共无效的onCreate(包savedInstanceState){
   如果(savedInstanceState!= NULL){
      字符串stringValue的= savedInstanceState.getString(钥匙);
      INT的intValue = savedInstanceState.getString(钥匙);
   }
   ...
}
 

注意:您也可以恢复数据的 onRestoreInstanceState() 的方法,但它不是普通(其称为后 ONSTART() 方式和 的onCreate() 被称为前)。

I am new to android application development, and can't understand what does bundle actually do for us.

Can anyone explain it for me?

解决方案

I am new to android application development and can't understand what does bundle actually do for us.. Can any one explain it for me. Thanks in advance.

In my own words you can image it like a MAP that stores primitive datatypes and objects as couple key-value

Bundle is most often used for passing data through various Activities. Provides putType() and getType() methods for storing and retrieving data from it.

Also Bundle as parameter of onCreate() Activity's life-cycle method can be used when you want to save data when device orientation is changed(in this case activity is destroyed and created again with non null parameter as Bundle).

More about Bundle at its methods you can read reference at developer.android.com where you should start and then make some demo applications to get experience.

Update:

Demonstration examples of usage:

Passing primitive datatypes through Activities:

Intent i = new Intent(ActivityContext, TargetActivity.class);
Bundle dataMap = new Bundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);

Passing List of values through Activities:

Bundle dataMap = new Bundle();
ArrayList<String> s = new ArrayList<String>();
s.add("Hello");
dataMap.putStringArrayList("key", s); // also Integer and CharSequence
i.putExtras(dataMap);
startActivity(i);

Passing Serialized objects through Activities:

public class Foo implements Serializable {

   private static final long serialVersionUID = 1L;

   private ArrayList<FooObject> foos;

   public Foo(ArrayList<FooObject> foos) {
      this.foos = foos;
   }

   public ArrayList<FooObject> getFoos() {
      return this.foos;
   }        
}


public class FooObject implements Serializable {

   private static final long serialVersionUID = 1L;

   private int id;

   public FooObject(int id) {
      this.id = id;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
}

Then:

Bundle dataMap = new Bundle();
ArrayList<FooObject> foos = new ArrayList<FooObject>();
foos.add(new FooObject(1));
dataMap.putSerializable("key", new Foo(foos));


Pass Parcelable objects through Activities:

There is much more code so here is article how to do it:


How to retrieve data in target Activity:

There is one magic method: getIntent() that returns Intent(if there are any data also with extended data) that started Activity from there method is called.

So:

Bundle dataFromIntent = getIntent().getExtras();
if (dataFromIntent != null) {
   String stringValue = dataFromIntent.getString("key");
   int intValue = dataFromIntent.getInt("key");
   Foo fooObject = (Foo) dataFromIntent.getSerializable("key");
   // getSerializble returns Serializable so we need to cast to appropriate object.
   ArrayList<String> stringArray = dataFromIntent.getStringArrayList("key");
}


Usage of Bundle as parameter of onCreate() method:

You are storing data in onSaveInstanceState() method as below:

@Override
public void onSaveInstanceState(Bundle map) {
   map.putString("key", "value");
   map.putInt("key", 1);
}

And restore them in onCreate() method(in this case is Bundle as parameter not null) as below:

@Override
public void onCreate(Bundle savedInstanceState) {
   if (savedInstanceState != null) {
      String stringValue = savedInstanceState.getString("key");
      int intValue = savedInstanceState.getString("key");
   }
   ...
}

Note: You can restore data also in onRestoreInstanceState() method but it's not common(its called after onStart() method and onCreate() is called before).

这篇关于在Android的是什么包是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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