分享

【Android基础】页面跳转与传值(Activity跳转与传值)

 black library 2011-10-29
 

【Android基础】页面跳转与传值(Activity跳转与传值)

分类: 【Android基础】 831人阅读 评论(3) 收藏 举报

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

 

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

 

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

 

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

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

也可以这样写:

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

 

只要这两句,就可以实现从A页面跳转到B页面了。  (A、B均继承自Activity)

 

有的时候,在跳转页面时还需要传递数据,这个时候如何做呢?

 

如果数据比较少,比如只要传一个名字,那么只要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. 首先,从A页面跳转到B页面时,不可以使用"startActivity()"方法,而要使用"startActivityForResult"方法。

2. 在A页面的Activity中,需要重写"onActivityResult"方法

  1. @Override  
  2. protected void onActivityResult(int requestCode,int resultCode,Intent data){  
  3.       
  4.     switch(requestCode){  
  5.     case RESULT_OK:  
  6.         /*取得来自B页面的数据,并显示到画面*/  
  7.         Bundle bundle = data.getExtras();  
  8.           
  9.         /*获取Bundle中的数据,注意类型和key*/  
  10.         String name = bundle.getString("Name");  
  11.         boolean ismale = bundle.getBoolean("Ismale");  
  12.     }  
  13. }  
 

3. 在B页面上加一个返回按钮,并在事件写如下代码:

  1. /*给上一个Activity返回结果*/  
  2. B.this.setResult(RESULT_OK,intent);  
  3. /*结束本Activity*/  
  4. B.this.finish();  

 

本讲就这么多。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多