android中数据传递常用到Intent。一方把数据存入Intent中,另一方取出。
今天,我在两个Acitivity之间传递数据时,一直收不到对方传来的数据。代码如下:
Intent intent = new Intent();
int source = intent.getIntExtra(FROMINFO, 0);
思考得知:
new Intent() 和 getIntent() 是不同的。
同:都可以得到一个intent对象
不同:new Intent() 注释:Create an empty intent
得到的是一个崭新的intent对象
getIntent() 注释:Return the intent that started this activity
得到的是上一个activity启动的intent,然后调用intent.getExtras()得到intent所附带的额外数据
那么,在intent.setClass()方法中,应该用哪种intent了?
实验知, 这两种方式在这个方法中是一样的。但为了清晰明白,建议使用new Intent()。