Intent 传递中 Bundle与intent.putExtra 的关系

来源:互联网 发布:union软件安卓版 编辑:程序博客网 时间:2024/04/30 03:36

有一篇文章介绍这两种东西的关系

 在开发软件的过程中,遇到过这样一种情况,就是没有create bundle,但是当你使用intent.putExtra之后,在另一个被call的activity中,会有bundle被传递过去,
原因就是因为intent.putExtra时,系统会检测有没有bundle,如果没有,则新建一个。所以下面这两个语句的等效的:

 

1

[java] view plaincopy
  1. Intent intent = new Intent(this,xxx.class);  
  2. intent.putExtra("test"true);  
  3.   
  4. startActivity(intent);  


2

 

[java] view plaincopy
  1. Intent intent = new Intent(this,xxx.class);  
  2.   
  3. Bundle bundle = new Bundle();  
  4.   
  5. bundle.putBoolean("test"true);  
  6.   
  7. intent.putExtras(bundle);  
  8.   
  9. startActivity(intent);  
另外一篇则是这样说的

[html] view plaincopy
  1. Intent intent = new Intent();  
  2. intent.putExtra("test_value", "TEST_V");  
[html] view plaincopy
  1. Bundle bundle = new Bundle();  
  2. bundle.putString("test_value", "TEST_V");  
  3. intent.putExtras(bundle);  
[html] view plaincopy
  1.    
[html] view plaincopy
  1. Bundle bundle = this.getIntent().getExtras();    


举个例子  我现在要从A界面   跳转到B界面或者C界面   
这样的话 我就需要写2个Intent  如果你还要涉及的传值的话 你的Intent就要写两遍添加值的方法 那么 如果我用1个Bundle  直接把值先存里边 然后再存到Intent中 不就更简洁吗?

另外一个例子  如果我现在有  Activity A ,B ,C;
现在我要把值通过A经过B传给C
你怎么传 如果用Intent的话   A-B先写一遍   再在B中都取出来 然后在把值塞到Intent中 再跳到C   累吗?
如果我在A中用了 Bundle 的话  我把Bundle传给B 在B中再转传到C  C就可以直接去了 
这样的话 还有一个好处 就是在B中 还可以给Bundle对象添加新的 key - value  同样可以在C中取出来


0 0
原创粉丝点击