Whats the difference between pass the variables like this:
Intent intent = new Intent(mCtx, DetailsActivity.class);
intent.putExtra("pId", id);
intent.putExtra("pType", type);
mCtx.startActivity(intent);
and using the keyword Bundle?
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
// Now let's Pass data using Bundle
Bundle bundle = new Bundle();
bundle.putString("pId", id);
bundle.putString("pType", type;
intent.putExtras(bundle);
startActivity(intent);
Im new to Android development and I am curious to which method is better or stadard when doing android development?
Intent
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
Bundle
public void putString(String key, String value) {
unparcel();
mMap.put(key, value);
}
putExtras does not put your bundle inside Intent. Instead, it copies it over to the current intent bundle.
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
The first method is recommended. All samples are using the first putExtra (key, value) methods.
In most scenarios you should know what you are passing to the next activity, so putExtra ( key, value ) makes sense because you know what the key is.
The only scenario I can think of to use the second method is a transient activity which receives bundle from previous activity and need to pass all information to next activity. The transient activity does not need to know what is being passed and just pass everything it received to the next activity.
BTW, if you create your own Bundle, put the key values in and then call putExtra ( bundle), there is extra cost for the temporary bundle so it is less efficient.
Whats the difference between pass the variables like this and using the keyword Bundle?
One puts the keys directly and one puts them in a Bundle first. The end result is the same.
Im new to Android development and I am curious to which method is better or stadard when doing android development?
The first is "better" or more "standard" simply because it eliminates the need to create the bundle yourself first - it's a redundant step. You'd really only ever need to use that method if your code already had some logic that maintained a Bundle of data that you wanted to pass along in an Intent as key / value pairs.
Related
How do you pass LiveData with intent to another Activity?
I'm trying to pass the LiveData object into a new Activity that uses ViewPager 2 to display one object at a time.
Here is Live data in the ViewModel
private LiveData<List<WrestlersEntity>> mWrestlersList;
public LiveData<List<WrestlersEntity>> getWrestlersList() {
return mWrestlersList;
}
Fragment passing live data.
adapter.setOnItemClickListener(wrestler -> {
Bundle bundle = new Bundle();
bundle.putSerializable("Value", (Serializable)mViewModel.getWrestlersList());
Intent addEditIntent = new Intent(getActivity(), AddEditWrestlerActivity.class);
addEditIntent.putExtras(bundle);
startActivityForResult(addEditIntent);
Pager Activity
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
List<WrestlersEntity> wrestler (List<WrestlersEntity>)bundle.getSerializable("Value");
I get the following error.
java.lang.ClassCastException: androidx.room.RoomTrackingLiveData cannot be cast to java.io.Serializable
If you can't cast live data as Serializable what are my other options to pass a LiveData> to a new activity.
here is a link to the git if you want to look at the full code.
https://github.com/Shawn-Nichol/Wrestlers
Your getWrestlersList() method returns a LiveData wrapping your List<WrestlersEntity>. What you want to pass to your Bundle is the List<WrestlersEntity> directly, but wrapped in a Serialized implementation of List. That's why the code below wraps the value of your LiveData in an ArrayList.
So, you can do this instead:
bundle.putSerializable("Value", new ArrayList<>(mViewModel.getWrestlersList().getValue()));
And this to read it back from the Bundle:
List<WrestlersEntity> wrestlers = (List<WrestlersEntity>)bundle.getSerializable("Value");
I have an Adapter which uses some custom object list. I wanted to pass the clicked object's to the other activity. So I made the object class implement Parcelable.
To send the data from the adapter
view.findViewById(R.id.play_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), YouTubePlayerActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", movie));
intent.putExtras(bundle);
mContext.startActivity(intent);
To receive the data in the destination activity
Bundle b = getIntent().getExtras();
ArrayList<Video> videos = b.getParcelable("data");
But when I run it, it would pass nothing.
I tried passing other simpler values like strings and integer and they were too not passed.
Then I had to achieve the task by creating interface. And it worked. But I still don't understand this unexpected behavior when starting activity from adapters. Can you please answer the reason behind this unexpected behavior?
I have several List<String> variables to pass from Splash to the Main activity:
1) I read somewhere that I can pass them as ArrayList<String> from Splash to Main, and it works... i.e.
I can receive only the first ArrayList<String> variable. In my bundle below, I am not able to receive the second ArrayList<String>. (array_list2) Why?
2) How to pass ArrayList<LatLng> from one activity to another
First Activity:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
b.putStringArrayList("array_list5",(ArrayList<LatLng>)coordinates); //ERROR in this line, type mismatch!
intent.putExtras(b);
startActivity(intent);
Second Activity:
Bundle b = getIntent().getExtras();
if (b != null) {
testList1 = b.getStringArrayList("array_list1");
testList2 = b.getStringArrayList("array_list2"); //THIS gives the same arraylist as testList1 and it is incorrect!
Log.e("TESTLIST1",testList1.toString()); //just using Log.e to view o/p as test
Log.e("TESTLIST2",testList1.toString());
Please answer both my questions. None of the other topics helped me, and I spent over 2 hours on this.
Thank you.
putStringArrayList() will not support for ArrayList .
use
putParcelableArrayList();
instead of
putStingArrayList();
or else you can use direct method of intent.
Change your code somethng like this:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
If you want to pass through bundle object use below line of code.
b.putParcelableArrayList("array_list5",(ArrayList<LatLng>)coordinates);
Otherwise pass through intent object.
intent.putParcelableArrayListExtra("array_list5",coordinates);
intent.putExtras(b);
startActivity(intent);
Because LatLng class implements Parcelable interface, so instead of using Bundle. putStringArrayList use Bundle.putParcelableArrayList to send ArrayList which contains class object which is implementing Parcelable interface. Use
b.putParcelableArrayList("array_list5",coordinates);
How to use parcelable with List or ArrayList - What is
the syntax?
Do it as:
ArrayList<String> arrCoordinates = new ArrayList<>(coordinates.size());
arrCoordinates.addAll(coordinates);
b.putParcelableArrayList("array_list5",arrCoordinates);
and get array_list5 as from Bundle
ArrayList<LatLng> coordinates = b.getParcelableArrayList("array_list5");
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()?
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");
}