Android study notes - transfer data between activities

In Activity, when there is often a need to transmit data, Intent is used to transmit data between two activities.
Intents can carry data, and intents have many overloads of putextra methods that can store data. Then in the activated activity, you can use the getIntent() method to get the intent that started the Activity, and then you can get data through this Intent.

public class MainActivity extends AppCompatActivity {
        Button button;
        EditText editText;
        public static final String SEND_MESG = "MSG";
        public static final int RESPONSE_CODE = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button) findViewById(R.id.btn_startactivity);
            editText = (EditText) findViewById(R.id.et_mssage);
            button.setOnClickListener(new View.OnClickListener() {//设置点击时间
                @Override
                public void onClick(View view) 
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra(SEND_MESG, editText.getText().toString());//使用Inent传入Sting数据
                    startActivityForResult(intent, RESPONSE_CODE);//使用startActivityForResult()开启一个新的活动,并且加入请求码,用以判断是哪个活动开启了新活动
                }
            });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //这个方法会在返回新的活动返回数据的时候调用,第一个参数是请求码,第二个参数是返回码,第三个参数是携带了回传数据的Intent
            if (requestCode == MainActivity. RESPONSE_CODE)
                switch (resultCode) {
                    case MainActivity.RESULT_OK: {
                        Toast.makeText(MainActivity.this, data.getStringExtra("data_return"), Toast.LENGTH_SHORT).show();
                    }
                }
        }

    }

Now for the second event

public class SecondActivity extends AppCompatActivity {
        TextView textView;
        EditText editText;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            button = (Button) findViewById(R.id.btn_response);
            editText = (EditText) findViewById(R.id.et_return);
            textView = (TextView) findViewById(R.id.tv_show);
            Intent intent = getIntent();
            String s = intent.getStringExtra(MainActivity.SEND_MESG);
            textView.setText(s);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.putExtra("data_return",editText.getText().toString());//同样,在Intent中放入EditText中输入的数据
                    setResult(MainActivity.RESULT_OK,intent);
                    //然后再调用setResult()方法设置返回值
                    finish();
                }
            });
        }
    }

The role of RequestCode The value of the
request code is set by itself and is used to identify the source of the request. For example: an Activity has two buttons. Clicking these two buttons will open the same Activity. No matter which button opens a new Activity, when the new Activity is closed, the system will call the onActivityResult(int requestCode, int resultCode, Intent of the previous Activity) data) method. In the onActivityResult() method, if you need to know which button the new Activity is opened by, and handle it accordingly;

The role of ResutCode
If you use the startActivityForResult() method to open multiple different activities, when these new activities are closed, the system will call the onActivityResult(int requestCode, int resultCode, Intent data) method of the previous activity. In order to know which new Activity the returned data comes from, you can do this in the onActivityResult() method (ResultActivity and NewActivity are the new Activity to be opened):

Simply put, RequestCode is used by the new activity to determine which activity is opened, and ResultCode is used to determine which new activity the returned data comes from.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325663846&siteId=291194637