使用Intent可以很方便在不同的Activity之间传递数据,这个也是官方推荐的方式,但是也有一定的局限性,就是 Intent无法传递不能序列化的对象 ,例如(图片)。我们可以使用静态变量来解决这个问题

Ø案例一

package com.android.myintent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Main extends Activity {/** Called when the activity is first created. */private Button button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) this.findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 声明一个意图Intent intent = new Intent();intent.setClass(Main.this, OtherActivity.class);OtherActivity.age = 23;OtherActivity.name = "jack";startActivity(intent);}});}}

package com.android.myintent;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private TextView textView;public static String name;public static int age;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);textView = (TextView)this.findViewById(R.id.msg);textView.setText("-name->>"+name+"\n"+"-age-->>"+age);}}

更多相关文章

  1. android 事件传递机制 【转】
  2. Android(安卓)数据传递-通过Intent传递数据
  3. Android(安卓)Home 长按键处理
  4. android SDK中java环境变量配置
  5. Android全局变量的定义与使用
  6. Android使用Ant批量打包
  7. Android四种常用的消息传递机制/模式的比较
  8. 反射类查看类下面的所有方法及变量
  9. Android(安卓)不得不知 之 Parcelable

随机推荐

  1. android 用Pull 创建XML
  2. Android(安卓)之 Gallery画廊用法
  3. Android(安卓)System.gc()与Runtime.getR
  4. android 下Excel操作
  5. android 判断网络是否可用,并调用系统设置
  6. Android(安卓)Wifi框架流程分析
  7. Android——分别获取导航栏、状态栏高度
  8. Android(安卓)配置 之 AndroidManifest.x
  9. Android输入法的显示与隐藏
  10. android 开发使用 kotlin 进行点击事件监