How to send more than 1 data with bundle ?
If only one :
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
a.putExtras(bundle);
startActivityForResult(a, 0);
if more than 1 data ??
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
what's next ??
just keep adding in bundle as you are adding bundle.putString("status", txtconfirm );
and when you are done set this bundle to intent:a.putExtras(bundle);
If I understood the question, this should be fine:
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm", txtconfirm);
for more than one data
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm",txtconfirm);
a.putExtras(bundle);
startActivityForResult(a, 0);
Just put your 2nd string in with bundle.putString() making sure you use a unique key name for it.
The process of serializing/parceling custom objects, attaching to the bundle with keys and undoing all this at the other end gets tedious when you have a lot of data or/and when the data needs to serve different purposes/functions in the launched Activity etc.
You can check out this library(https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) I wrote to try and solve this problem.
GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.
You can also trigger different functionalities in the Activity directly by choosing which method to launch into with the data.
Related
I have a sign-up form, and I have to pass all info filled in that form to another screen. I know how to display if there is one field, but in sign-up form there are multiple fields. so I want to know how to display all the info.
If you are launching a new activity, just create a Bundle, add your values, and pass it into the new activity by attaching it to the Intent you are using:
/*
* In your first Activity:
*/
String value = "something you want to pass along";
String anotherValue = "another something you would like to pass along";
Bundle bundle = new Bundle();
bundle.putString("value", value);
bundle.putString("another value", anotherValue);
// create your intent
intent.putExtra(bundle);
startActivity(intent);
/*
* Then in your second activity:
*/
Bundle bundle = this.getIntent().getExtras();
String value = bundle.getString("value");
String anotherValue = bundle.getString("another value");
To pass User data(multiple info) from one screen to another screen :
Create a model for user with setter and getter method.
make this class Serializable or Parcelable (Prefer) .
Create object of user class and set all data using setter method.
Pass this object from one activity to another by using putSerializable.
Person mPerson = new Person();
mPerson.setAge(25);
Intent mIntent = new Intent(Activity1.this, Activity2.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
And get this object from activity 2 in on create methode.
Person mPerson = (Person)getIntent().getSerializableExtra(SER_KEY);
and SER_KEY will be same.
for more detail please go to this link:
http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html
I hope it will work for you.
You can make use of bundle for passing values from one screen to other
Passing a Bundle on startActivity()?
Is there any way to determine if a specific boolean value was sent via an Intent? In other words, I know how to send data via an Intent and how to read it back, but I would like to know if data was sent.
The current getBooleanExtra method requires a default value, so I can't check if it wasn't sent by using this.
I currently have this:
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
If the Extras.EXTRA_SHOW_NAVIGATION_DRAWER value wasn't set at all, I'd like to do some extra work. Obviously if I get true it means it was sent, however if I get false there's no way to tell.
This can be done extracting the intent bundle:
Bundle b = getIntent().getExtras();
boolean hasNavDrawerSetting = b.containsKey(Extras.EXTRA_SHOW_NAVIGATION_DRAWER);
if (hasNavDrawerSetting) {
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
} else {
showNavigationDrawer = getResources().getBoolean(R.bool.hasSideMenu);
}
If you really want to check whether your value is coming or not then you can take help of Bundle object. You can pass your boolean value through a bundle object. If the bundle object is null then no value is received in the next activity. But if it is not null then you will surely receive the value of your boolean parameter only if you put it in the bundle. It may be true or false depending upon the value you set. Below I'm providing two code snippets. One is for calling activity and another is one for called activity.
Calling Activity -->
Intent intent = new Intent(this,Experimental.class);
Bundle b = new Bundle();
b.putBoolean("key",true);
startActivity(intent.putExtra("bundle",b));
Called Activity -->
setContentView(R.layout.experimental);
Bundle b = getIntent().getBundleExtra("bundle");
if(b != null){
Log.d("Value",String.valueOf(b.getBoolean("key",false)));
}
Hope I'm able to help you.
I want to pass a value from the ListView Activity 1 Activity 2 for editing.
I have this code but the value is not passed in the second Activity.
ACTIVITY A
Intent i = new Intent(this, Modifica_entrate.class);
Bundle extras = new Bundle();
extras.putString (tv1.getText().toString(), data);
i.putExtras(extras);
ACTIVITY B
Bundle extras = getIntent().getExtras();
String valuePass = extras.getString("data");
mDataScelta.setText(i.getExtras().getString(valuePass));
You are mixing up keys and values a bit too much.
The first parameter here:
extras.putString (tv1.getText().toString(), data);
Must match the parameter here:
String valuePass = extras.getString("data");
So the code you have there puts a String with the key tv1.getText().toString() that is, it takes the text you entered in the textbox and uses it as a key (which is probably not what you intended to do). For this key, you are putting the value of the variable data. Then you try to retrieve the key "data" (note also that data and "data" are not the same thing).
So what you want is probably:
extras.putString("data", tv1.getText().toString());
And then you can retrieve it like this:
mDataScelta.setText(i.getStringExtra("data"));
I am currently attempting to pass a string across activities. The string is used to set an image in the second activity. So the user is clicking a button in the first activity and then the appropriate image is loaded in the second activity based upon what was selected. There are three buttons in the first activity all which should send a String value to the next activity.
This is what I have in my first activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selection = "archer";
Bundle basket = new Bundle();
basket.putString("key", selection);
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtras(basket);
startActivity(i);
finish();
}
});
}
The receiving activity has the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
ImageView img = (ImageView) findViewById(R.id.Character_Chosen);
Bundle extras = getIntent().getExtras();
String Toon = extras.getString("key");
if(Toon=="archer"){
img.setImageResource(R.drawable.archer);
}
if(Toon=="mage"){
img.setImageResource(R.drawable.mage);
}
if(Toon=="warrior"){
img.setImageResource(R.drawable.warrior);
}
}
I know that the receive if statements section works because if I manually set the value Toon the image will load. So somewhere the information is not being sent or read properly. I followed a tutorial online and but am stumped on this.
issue is Java 101. string equality test is done using the equals method.
Toon.equals("archer");
Extra tip : inverse your test to avoid null pointer exception :
"archer".equals(Toon);
Extra extra tip : considering your cases are mutually exclusive, use if else if to avoid testing all cases.
You are comparing your strings wrong,
Toon=="Anything"
will never be true, you need to use
Toon.equals("Anything")
for it to work.
EDIT:
This is true because "==" will just compare the memory addresses of the Strings, and "Anything" will just have been allocated and definitely won't match Toon. .equals however actually compares the chars in the string.
Also to test things like this try putting in a
Log.i("YourAppName", "Toon value is: " + Toon);
after
String Toon = extras.getString("key");
try to use SharedPreferences, Set it after/with the button-click and get it on 2. Activity OnCreate
If you just want to pass a simple String, you can use the following:
On sending:
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtra("key", selection);
startActivity(i);
On receiving:
String selection = getIntent().getStringExtra("key");
If there is no parameter with this key, selectionwill be null.
Try this
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("email", "lol#hotmail.com");
startActivity(i);
and
Intent i = getIntent();
String myemail = i.getStringExtra("email");
Furthermore, to compare String, use .equals() not ==
I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}