Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a value from one Activity to another in Android?

Tags:

android

I have created an Activity with a AutuCompleteTextView[ACTV] and button. I enter some text in the ACTV then press the button. After I press the button I want the Activity to go to another Activity. In the second Activity I just want to display the text entered in ACTV(of the first actvity) as a TextView.

I know how to start the second activity which is as below:

Intent i = new Intent(this, ActivityTwo.class); startActivity(i); 

I have coded this to obtain the text entered from the ACTV.

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete); CharSequence getrec=textView.getText(); 

My question here is how to pass "getrec" (after I press the button) from the first Activity to the second. And later recieve "getrec" in the second activity.

Please assume that I have created the event handler class for the button by using "onClick(View v)"

like image 948
Santosh V M Avatar asked Aug 18 '10 09:08

Santosh V M


People also ask

How can I transfer data from one activity to another in Android?

We can send the data using putExtra() method from one activity and get the data from the second activity using getStringExtra() methods. Example: In this Example, one EditText is used to input the text. This text is sent to the second activity when the “Send” button is clicked.

How do you pass a value from one activity to another?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can I pass multiple EditText values to another activity in Android?

You need put them in Extras (putExtras) and then pass from the current activity to the other one. You need capture your EditText value as String and then putExtra with Key - one each for your need and then retrieve them in the second activity.


1 Answers

You can use Bundle to do the same in Android

Create the intent:

Intent i = new Intent(this, ActivityTwo.class); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete); String getrec=textView.getText().toString();  //Create the bundle Bundle bundle = new Bundle();  //Add your data to bundle bundle.putString(“stuff”, getrec);  //Add the bundle to the intent i.putExtras(bundle);  //Fire that second activity startActivity(i); 

Now in your second activity retrieve your data from the bundle:

//Get the bundle Bundle bundle = getIntent().getExtras();  //Extract the data… String stuff = bundle.getString(“stuff”);  
like image 159
DeRagan Avatar answered Sep 22 '22 18:09

DeRagan