How to pass a value from one Activity to another in Android?
Solution 1:
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”);
Solution 2:
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);
Solution 3:
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.
Solution 4:
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");
Solution 5:
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.