• 3.Android开发笔记:Activity 数据传递


    1.向下一个Activity传输数据

    1.1 传递普通类型数据

    向下一个Activity传输数据:

    Intent intent5 = new Intent(FirstActivity.this,SecondActivity.class);
    intent5.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
    startActivity(intent5);
    
    

    提取上一个Activity的数据

    Intent intent = getIntent();
    String data = intent.getStringExtra("key_ExtData");
    Log.i("key_ExtData", data);
    Toast.makeText(SecondActivity.this, data,Toast.LENGTH_LONG).show();
    

    1.2 传递对象

    比如要传递的数据类型是:类Device, 则该类要继承类Serializable

    Device.java

    
    public class Device implements Serializable {
    
        private String num;
        private String name;
    
        //get and set
        ......
    
    

    SecondActivity.java

    • 传入对象:intent.putExtra(key,object);
    • 获取对象:intent.getSerializableExtra(key);
    
    public class SecondActivity extends AppCompatActivity {
    
        private static final String KEY_INTENT_DEVICE ="keyIntent_device";
      
        /**
         * 提供一个静态方法,用于开启该Activity
         * */
        public static void startActivity(Context context, Device device){
            Intent intent = new Intent(context, SecondActivity.class);
            intent.putExtra(KEY_INTENT_DEVICE, device);
            context.startActivity(intent);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            LogUtil.v(TAG, "onCreate");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_secondactivity );
    
            Device device = (Device) getIntent().getSerializableExtra(KEY_INTENT_DEVICE);
        }
    
        ...
    }
    

    FirstActivity.java

    
     Device device = new Device();
     SecondActivity.startActivity(mContext ,device);
    
    

    2.返回数据给上一个Activity

    1. 跳转使用:startActivityForResult()方法接收两个参数,
      第一个参数还是Intent,
      第二个参数是请求码,用于在之后的回调中判断数据的来源。

    跳转到SecondActivity,请求码是2

    Intent intent6 = new Intent(FirstActivity.this, SecondActivity.class);
    intent6.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
    startActivityForResult(intent6, 2);//请求码是2
    

    跳转到ThirtActivity,请求码是3

    Button button3 = findViewById(R.id.button1_2);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(FirstActivity.this, ThirtActivity.class);
            intent.putExtra("key_ExtData", "Hello,I'm from FirstActivity!!!");
            startActivityForResult(intent, 3); //请求码是3
        }
    });
    
    1. 在下一个Activity中返回数据

      注意:使用setResult(请求码,intent);返回数据

      Intent intent = new Intent();
      intent.putExtra("return_data","MSG: I'm From SeconActivity~!~~~");
      setResult(2,intent); //请求码是2
      finish();
      
      Intent intent  = new Intent();
      intent.putExtra("return_data", "MSG:I'm From 3th Activity!~~~~");
      setResult(3, intent); //请求码是3
      finish();
      
    2. 在FirstActivity重写响应事件 onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) { //请求码
            case 2: { 
                String string = data.getStringExtra("return_data");
                Log.d("onActivityResult", string);
    
                break;
            }
            case 3: {
                String string = data.getStringExtra("return_data");
                Log.d("onActivityResult", string);
    
                break;
            }
            default:
                Log.e("onActivityResult", "no such requestCode:" + requestCode);
                break;
        }
    }
    

    如果用户在SecondActivity中并不是通过点击按钮,
    而是通过按下Back键回到FirstActivity,这样数据不就没法返回了吗?
    我们可以通过在SecondActivity中重写onBackPressed()方法来解决这个问题,

    public class ThirtActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_thirt);
    
            Button button  = findViewById(R.id.button3);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setInfoBack();
                }
            });
        }
    
        @Override
        public void onBackPressed() {
            setInfoBack();
        }
    
        private void setInfoBack(){
            Intent intent  = new Intent();
            intent.putExtra("return_data", "MSG:I'm From 3th Activity!~~~~");
            setResult(3, intent);
            finish();
        }
    }
    
  • 相关阅读:
    hadoop的namenode故障处理方法
    hadoop的checkpoint检查时间参数设置
    hadoop配置历史服务器&&配置日志聚集
    hadoop_批量命令脚本&同步文件脚本
    查看centos操作系统、java_jdk、hadoop位数
    hadoop的namenode启动失败
    shell_语法
    Mycat入门配置_读写分离配置
    VM虚拟机克隆_修改网络
    ssh免密登陆及时间设置
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/12391956.html
Copyright © 2020-2023  润新知