android 数据传输json android数据传递方式_android


(原谅我,上图盗自bilibili明日科技)

其实这个图现在是有点不全面的,还有直接从数据到Intent(意图)

android 数据传输json android数据传递方式_android_02

从数据---Bundle---Intent 和数据---Intent两种方式我们分开说,也方便对比

1、数据—Bundle—Intent

(1)发送数据:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("key1","value1");
bundle.putInt("key",Value);
intent.putExtras(intent);
startActivity(intent);
(2)接收数据
Intent intent = getIntent();
Bundle bundle = getExtras();
String value1 = bundle.getString("key");
int value = bundle.getInt("key1");
注意观察,这样前后是对应的,Put过去就get过来,这是一个互逆的过程

2、数据–Intent

(1)发送数据

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Intent.putExtra("key1","value1");
intent.putExtra("key2",value2);
//无论传递过去的类型是什么都用putExtra,很方便的吧
//我也觉得用这种方式传递过去很方便

(2)接收数据

Intent intent = getIntent();
String value1 = intent.getStringExtra("key1");
int value2 = intent.getIntExtra("key2");
//看出来啥区别木,数据--Intent这种方式少了中间的Bundle过程,但是在接受的时候getIntExtra就多了一个Extra
//我觉得这也是bundle的体现,这种方式应该是对Bundle的默认省略
//我猜的,只是方便个人的理解
就是这样喽,拜拜