How to get data from other activity in android?

后端 未结 5 1109
-上瘾入骨i
-上瘾入骨i 2020-12-31 19:47

I have two activities such as Activity A and B and I\'m trying to pass two different strings from A to B using Bundle and startActivity(inten

5条回答
  •  借酒劲吻你
    2020-12-31 20:31

    // First activity
    actvty_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {               
            Intent i = new Intent(v.getContext(),SECONDACTIVITY.class);    
            startActivityForResult(i, STATIC_INTEGER_VALUE);
        }
    });
    
    /* This function gets the value from the other activity where we have passed a value on calling this activity */ 
    public void activity_value() {
        Intent i = getIntent();
        Bundle extras=i.getExtras();
        if(extras !=null) {
            // This is necessary for the retrv_value
            rtrv_value = extras.getString("key");
    
            if(!(rtrv_value.isEmpty())) {
                // It displays if the retrieved value is not equal to zero
                myselection.setText("Your partner says = " + rtrv_value);
            }
        }
    }
    
    
    // Second activity
    myBtn.setOnClickListener(new View.OnClickListener () {
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
            Bundle bundle = new Bundle();
            bundle.putString("key", txt1.getText().toString());
            // Here key is just the "Reference Name" and txt1 is the EditText value
            intent.putExtras(bundle);               
            startActivity(intent);
        }
    });
    

提交回复
热议问题