一个Android应用程序很少会只有一个Activity对象,如何在多个Activity之间进行跳转,而且能够互相传值是一个很基本的要求。

本次我们就讲一下,Android中页面跳转以及传值的几种方式!

Activity跳转与传值,主要是通过Intent类来连接多个Activity,通过Bundle类来传递数据。

最常见最一般的页面跳转代码,很简单,如下:

Intent intent = new Intent(A.this, B.class);
startActivity(intent);
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);

如果数据比较少,比如只要传一个名字,那么只要j加一句"intent.putExtra("Name", "feng88724");"即可,代码如下:

1         Intent intent = new Intent();
2         intent.setClass(A.this, B.class);
3         intent.putExtra("Name", "feng88724");
4         startActivity(intent);

如果数据比较多,就需要使用 Bundle类了,代码如下: (说明直接看注释)

 1         Intent intent = new Intent(A.this, B.class);
 2         
 3         /* 通过Bundle对象存储需要传递的数据 */
 4         Bundle bundle = new Bundle();
 5         /*字符、字符串、布尔、字节数组、浮点数等等,都可以传*/
 6         bundle.putString("Name", "feng88724");
 7         bundle.putBoolean("Ismale", true);
 8         
 9         /*把bundle对象assign给Intent*/
10         intent.putExtras(bundle);
11         
12         startActivity(intent);

以上我们讲的都是如何进行页面跳转及数据传递,那么在另一个页面B上,应该如何接收数据呢?

在A页面上是以Bundle封装了对象,自然在B页面也是以Bundle的方式来解开封装的数据。主要通过"getIntent().getExtras()"方法来获取Bundle,然后再从Bundle中获取数据。 也可以通过" this.getIntent().getStringExtra("Name");"方法直接从Intent中获取数据。

从Bundle获取数据的代码:

 1     @Override
 2      public void onCreate(Bundle savedInstanceState) {
 3             super.onCreate(savedInstanceState);
 4             /*加载页面*/
 5             setContentView(R.layout.main);
 6             
 7             /*获取Intent中的Bundle对象*/
 8             Bundle bundle = this.getIntent().getExtras();
 9             
10             /*获取Bundle中的数据,注意类型和key*/
11             String name = bundle.getString("Name");
12             boolean ismale = bundle.getBoolean("Ismale");
13             
14     }

有时,在页面跳转之后,需要返回到之前的页面,同时要保留用户之前输入的信息,这个时候该怎么办呢?

在页面跳转后,前一个Activity已经被destroy了。如果要返回并显示数据,就必须将前一个Activity再次唤醒,同时调用某个方法来获取并显示数据。

要实现这个效果,需要做以下几步:

1.跳转的时候不是采用startActivity(intent) 这个方法,而是startActivityForResult(intent, 0)。

Intent intent=new Intent();
intent.setClass(A.this, B.class);
Bundle bundle=new Bundle();
String str1="aaaaaa";
bundle.putString("str1", str1);
intent.putExtras(bundle);
startActivityForResult(intent, 0);//这里采用startActivityForResult来做跳转,此处的0为一个依据,可以写其他的值,但一定要>=0

2.重写onActivityResult方法,用来接收B回传的数据。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 switch (resultCode) { //resultCode为回传的标记,我在B中回传的是RESULT_OK
     case RESULT_OK:
     Bundle b=data.getExtras(); //data为B中回传的Intent
      String str=b.getString("str1");//str即为回传的值
    break;
    default:
      break;
   }
 }

3.在B中回传数据时采用setResult方法,并且之后要调用finish方法。

setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent,当然也可以自己定义新的Bundle
finish();//此处一定要调用finish()方法
 

这样当B中调用finish方法的时候,跳转到A时会自动调用onActivityResult方法,来获取B中回传的intent了。 

 

 

PS:来自http://www.cnblogs.com/feng88724/archive/2011/02/10/1961225.html

http://www.cnblogs.com/leon19870907/articles/2010701.html

posted on 2014-09-17 15:26  蒋晓宇  阅读(349)  评论(0编辑  收藏  举报