Home > front end >  How to pass values from one activity to another in android studio
How to pass values from one activity to another in android studio

Time:11-25

I'm new to android studio, I came across this multiple contact picker and I want to get phone numbers in 1st activity and in the 2nd activity send messages to those selected numbers.

In my first activity I have -

new MultiContactPicker.Builder(MainActivity.this) //Activity/fragment context
                        .theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
                        .hideScrollbar(false) //Optional - default: false
                        .showTrack(true) //Optional - default: true
                        .searchIconColor(Color.WHITE) //Option - default: White
                        .setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
                        .handleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
                        .bubbleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
                        .bubbleTextColor(Color.WHITE) //Optional - default: White
                        .setTitleText("Select Contacts") //Optional - default: Select Contacts
                        .setSelectedContacts("10", "5" / myList) //Optional - will pre-select contacts of your choice. String... or List<ContactResult>
                        .setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
                        .limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone   email, limiting to one can improve loading time)
                        .setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                                android.R.anim.fade_in,
                                android.R.anim.fade_out) //Optional - default: No animation overrides
                        .showPickerForResult(CONTACT_PICKER_REQUEST);

The numbers get stored in results which is also in 1st activity -

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST){
    if(resultCode == RESULT_OK) {
        List<ContactResult> results = MultiContactPicker.obtainResult(data);
        Log.d("MyTag", results.get(0).getDisplayName());
    } else if(resultCode == RESULT_CANCELED){
        System.out.println("User closed the picker without selecting items.");
    }
}
}

I called the results in 2nd activity -

 List<ContactResult> results = new ArrayList<>();

But when I print the output, it doesn't give any. How can I get it right. Thanks in advance.

CodePudding user response:

You can set up intents when you are starting a new activity. Intents are a way to pass values from your current activity to the next and I'd suggest you learn the basics uses for them. Here's official documentation you can check out: https://developer.android.com/guide/components/intents-filters

When you start the other activity you can code the Intent like this:

//Code to start an activity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("id", "YOUR_VALUE1");
        intent.putExtra("title", "YOUR_VALUE2");
        startActivity(intent);

On the SecondActivity.class you will get can then assign your value to a variable:

//Code on your second activity
public class FollowersActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent = getIntent();
            String id = intent.getStringExtra("id");
            String title = intent.getStringExtra("title");
            
            //id will equal "YOUR_VALUE1"
            //title will equal "YOUR_VALUE2"

CodePudding user response:

First activity sent your data:- // if you want to send on selected position data than pass position in place of zero (0).

Inent intent = new Intent(this, SecondActivity.class)
        intent.putExtra("name", results.get(0).getName());
        intent.putExtra("number", results.get(0).getNumber());
        startActivity(intent)

// if you want to send the all the list in second activity using intent than

Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", results);
intent.putExtras(bundle);
startActivity(intent);

Second activity get your data:-

// retrive selected position data

Intent intent = new Intent();
 String name = intent.getStringExtra("name").toString();
        String number = intent.getStringExtra("number").toString();
        tv_name2.setText(name);
        tv_number.setText(number);

// retrive result list from intent

Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");

hope this answer will helpful for you...

  • Related