两个Activity之间通过Intent相互(双向)传递数据

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fuckluy/article/details/50933526

两个Activity通过Intent相互交换数据,在Activity中用一个Button 选择是否接受数据,不能直接写在onCreate中否则在Activity1 中获取从Activity2发送的数据时会崩溃,原因是因为在Activity中会通过Intent再创建一个Activity1的界面 而Activity1在内存中是存在的。


package com.example.he.text2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by he on 2016/3/19.
 */
public class FirstActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstlayout);
        Button first_button =(Button)findViewById(R.id.first_button);
        Button second_button=(Button)findViewById(R.id.second_button);

        //向SecondActivity传数据的按钮
        first_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("first_action","hello secondActivity");
                startActivity(intent);
            }
        });

        //从SecondActivity得到数据的按钮
        second_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);
            }
        });


    }



    //读取返回信息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case  1:
                if (resultCode==RESULT_OK){
                    String s=data.getStringExtra("data_return");
                    Toast.makeText(FirstActivity.this, s, Toast.LENGTH_SHORT).show();
//                    Log.i("FirstActivity",s);
                }
                break;
        }
    }
}



package com.example.he.text2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by he on 2016/3/19.
 */
public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlayout);

        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent();
                intent2.putExtra("data_return", "hello firstActivity");
                setResult(RESULT_OK, intent2);
                finish();
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = getIntent();
                String data = intent1.getStringExtra("first_action");
                Toast.makeText(SecondActivity.this, data, Toast.LENGTH_SHORT).show();
//                Log.i("SecondActivity", data);
            }
        });

    }


    @Override
    public void onBackPressed() {
        Intent intent2 = new Intent();
        intent2.putExtra("data_return", "hello firstActivity");
        setResult(RESULT_OK, intent2);
        finish();
    }
}



展开阅读全文

没有更多推荐了,返回首页