[android] How to pass a value from one Activity to another in 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)"

This question is related to android

The answer is


Standard way of passing data from one activity to another:

If you want to send large number of data from one activity to another activity then you can put data in a bundle and then pass it using putExtra() method.

//Create the `intent`
 Intent i = new Intent(this, ActivityTwo.class);
String one="xxxxxxxxxxxxxxx";
String two="xxxxxxxxxxxxxxxxxxxxx";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“ONE”, one);
bundle.putString(“TWO”, two);  
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);

otherwise you can use putExtra() directly with intent to send data and getExtra() to get data.

Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);

Thats trivial, use Intent.putExtra to pass data to activity you start. Use then Bundle.getExtra to retrieve it.

There are lots of such questions already https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+another+in+Android be sure to use search first next time.


Its simple If you are passing String X from A to B.
A--> B

In Activity A
1) Create Intent
2) Put data in intent using putExtra method of intent
3) Start activity

Intent i = new Intent(A.this, B.class);
i.putExtra("MY_kEY",X);

In Activity B
inside onCreate method
1) Get intent object
2) Get stored value using key(MY_KEY)

Intent intent = getIntent();
String result = intent.getStringExtra("MY_KEY");

This is the standard way to send data from A to B. you can send any data type, it could be int, boolean, ArrayList, String[]. Based on the datatype you stored in Activity as key, value pair retrieving method might differ like if you are passing int value then you will call

intent.getIntExtra("KEY");

You can even send Class objects too but for that, you have to make your class object implement the Serializable or Parceable interface.

TransactionTooLargeException

How much data you can send across size. If data exceeds a certain amount in size then you might get TransactionTooLargeException. Suppose you are trying to send bitmap across the activity and if the size exceeds certain data size then you might see this exception.


Sample Kotlin code as below:-

Page 1

val i = Intent(this, Page2::class.java)
            val getrec = list[position].promotion_id
            val bundle = Bundle()
            bundle.putString("stuff", getrec)
            i.putExtras(bundle)
            startActivity(i)

Page 2

        var bundle = getIntent().getExtras()
        var stuff = bundle.getString("stuff")

Implement in this way

String i="hi";
Intent i = new Intent(this, ActivityTwo.class);
//Create the bundle
Bundle b = new Bundle();
//Add your data to bundle
b.putString(“stuff”, i);
i.putExtras(b);
startActivity(i);

Begin that second activity, inside this class to utilize the Bundle values use this code

Bundle bundle = getIntent().getExtras();
String text= bundle.getString("stuff");

in the first Activity:

Intent i=new Intent(getApplicationContext,secondActivity.class);

i.putExtra("key",value);

startActivity(i);

and in the SecondActivity:

String value=getIntent.getStringExtra("Key");