其他分享
首页 > 其他分享> > Android Intent Activity之间传递数据

Android Intent Activity之间传递数据

作者:互联网

向下传递

FirstActivity → ThirdActivity

FirstActivity

button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //数据样式
                String data = "Hello Next!";
                //显式Intent
                Intent intent = new Intent(FirstActivity.this,ThirdActivity.class);
                //以键值对的方式存入数据
                intent.putExtra("FirstActivity_data",data);
                //启动Intent
                startActivity(intent);
            }
        });

ThirdActivity

//获取启动此Activity的Intent
Intent intent = getIntent();
//根据传递的数据类型选择相应的Extra方法,传入对应的键,获取值
String data = intent.getStringExtra("FirstActivity_data");
//输出日志查看
Log.d(TAG, "onCreate: "+data);

向上传递

ThirdActivity → FirstActivity

FirstActivity

private static final String TAG = "FirstActivity";
    //重写onActivityResult方法,接收返回数据
    //requestCode:启动ThirdActivity的请求码
    //resultCode:ThirdActivity返回活动FirstActivity的返回码
    //data:带有返回数据的intent
    @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //匹配请求码
    switch (requestCode) {
        case 1:
            //匹配返回码
            if (resultCode == RESULT_OK) {
                //取出数据
                String return_data = data.getStringExtra("ThirdActivity_data");
                //输出日志查看
                Log.d(TAG, "onActivityResult: "+return_data);
            }
        default:
    }
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    Button button_1 = findViewById(R.id.button_1);
    button_1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //显式 Intent
            Intent intent = new Intent(FirstActivity.this,ThirdActivity.class);
            //启动Intent,并设置请求码为1
            startActivityForResult(intent,1);
        }
    });
}

ThirdActivity

按键

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_layout);
    Button button_1 = findViewById(R.id.third_button_1);
    button_1.setOnClickListener(new View.OnClickListener() {
    @Override
    	public void onClick(View v) {
            //数据样式
            String data = "Hello My Is ThirdActivity.onCreate.onClick";
            //创建一个空的intent
            Intent intent = new Intent();
            //将数据按键值对的方式存储于intent
            intent.putExtra("ThirdActivity_data",data);
            //为该intent设置返回码
            setResult(RESULT_OK,intent);
            //销毁该Activity
            finish();
        }
    });
}

Back

public void onBackPressed() {
    String data = "Hello My Is ThirdActivity.onBackPressed";
    Intent intent = new Intent();
    intent.putExtra("ThirdActivity_data",data);
    setResult(RESULT_OK,intent);
    finish();
}

标签:ThirdActivity,FirstActivity,void,Intent,传递数据,intent,Android,data
来源: https://www.cnblogs.com/lisztomania/p/12844035.html