Android_Intent的使用方法

Intent可以在2个activity之间传递参数。

1.简单数据的传递

currentActivity.java中的部分代码

Intent intent = new Intent(CurrentActivity.this, targetActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("num1", num1);
			bundle.putDouble("num2", num2);
			bundle.putDouble("num3", num3);
			bundle.putString("sign", sign);
			
			intent.putExtras(bundle);
			startActivity(intent);

调用startActivity方法时,开始跳转activity

secondActivity.java中的部分代码

Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		Double num1 = bundle.getDouble("num1");
		Double num2 = bundle.getDouble("num2");
		Double num3 = bundle.getDouble("num3");
		String sign = bundle.getString("sign");

2.传递对象

currentActivity.java中的部分代码

//封装对象(对象已实现序列化)
			Person person = new Person("Raise", "male", 23);
			Intent intent = new Intent(CounterActivity.this, CounterSecond.class);
			Bundle bundle = new Bundle();
			//存放序列化对象
			bundle.putSerializable("person", person);
			intent.putExtras(bundle);
			startActivity(intent);

调用startActivity方法时,开始跳转activity

secondActivity.java中的部分代码

//获取bundle对象
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		Person person = (Person) bundle.getSerializable("person");


你可能感兴趣的:(android)