Android Intent Messenger

//基本使用
Intent intent = new Intent(本地上下文, B.class);
intent.putExtra(key,value);
startActivity(intent);

//基本使用  B类里面
Intent intent = getIntent();
int a =intent.getIntExtra(key,-1);//取什么值类型一定要对上
//取布尔类型
boolean a = intent.getBooleanExtra(key,false);
startActivity(intent);




//使用Bundle传值
Intent intent = new Intent(本地上下文, B.class);
Bundle bundle = new Bundle();
bundle.putSerializable("arrList", (Serializable) arrList);
intent.putExtra("bundleData", bundle);

//取值
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundleData");
ArrayList<info>list=(ArrayList<info>)bundle.getSerializable("arrList");


//其实,每次Intent跳到别的activity时,再回来已经不是原来的activity了,应对这种情况
//解决方案
A页面跳转信使
 startActivityForResult(intent, 0);

B页面数据操作完成回到A页面
setResult(RESULT_OK, intent);
finish();//此处一定要调用finish()方法

A页面的 接受B页面的数据
  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {}}

 

Guess you like

Origin blog.csdn.net/qq_41630695/article/details/108799639