This question already has an answer here:
Passing arraylist in intent from one activity to another
(1 answer)
Closed 6 years ago.
I want to send a ArrayList with intent for starting new activity.
I have something like this-
ArrayList<LatLng> positions = new ArrayList<LatLng>() ;
positions.add(new LatLng(12,13));
positions.add(new LatLng(13,14));
positions.add(new LatLng(14,15));
Intent intent = new Intent(Current.this,Next.class);
Now I want to carry this ArrayList named positions to the Next activity and want to use there. How can I achieve it.
LatLng class already implements Parcelable So you can directly pass this arraylist as
intent.putParcelableArrayListExtra("key", value)
you can use
intent.putExtra("array",positions.toArray());
Related
This question already has answers here:
Passing custom objects between activities?
(5 answers)
Closed 5 years ago.
I want to send user define object from one activity to another in android application.
I have created user class object and send this user object to my second activity from first activity.
Implement your class with Serializable interface.
Then pass the object using
intent.putExtra("MyClass", obj);
and retrieve object by calling
getIntent().getSerializableExtra("MyClass");
See this post
Make sure your User class implements Parcelable.
public class User implements Parcelable {
...........
...............
}
Send User object to SecondActivity as below:
User userObject = new User();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("user_data", userObject);
startActivity(intent);
Retrieving the User object in SecondActivity.
User user = (User) getIntent().getParcelableExtra("user_data");
Here is good Tutorial about using Parcelable.
Hope this will help~
Iplemented my user define class to Parcelable interface.
This question already has answers here:
Android:Passing a hash map between Activities
(5 answers)
Closed 8 years ago.
I have some data in Map collection.I want to send and retrieve it in next activity
exa.
Map data=new HashMap();
data.put("name","abc");
data.put("status","Busy in work");
Intent intent = new Intent(MainActivity.this, SmsVerActivity.class);
//pass Map collection data to next activity
intent.putExtra("vercode",data.get("vercode").toString());
intent.putExtra("cname",data.get("cname").toString());
intent.putExtra("status_txt",data.get("status_txt").toString());
startActivity(intent);
Is there any shortcut way to do this ??
I mean can we pass Like below ??
intent.putExtra("data",data);
So that i can retrieve it in SmsVerActivity as
Bundle extras=getintent().getExtras();
//get data from MainActivity
String name=extras.getCharSequence(data.get("name"));
String status=extras.getCharSequence(data.get("status_txt"));
If you use HashMap implementation you can use
bundle.putExtra("map", (Serializable) map);
because HashMap implements Serializable interface.
How to retrieve it in another Activity?
map = (Map) getIntent().getExtras().getSerializable("map");
I am trying to send an array of objects to a new intent, but the problem is that I don't know how to send the whole array at once. The method I managed to get it working is adding object by object, in a for loop, and in the second activity I have to get them one by one, something like this:
Main activity:
Intent newIntent = new Intent(mainA.this, secondA.class);
for(int i=0;i<numberOfObjects;i++)
newIntent.putExtra("object"+i,myObjects[i]);
newIntent.putExtra("length", x);
startActivity(newIntent);
Second activity:
Serializable n = getIntent().getSerializableExtra("length");
int x = Integer.parseInt(n.toString());
myObjects = new objClass[x];
for(int i=0;i<x;i++)
myObjects[i] = (objClass) getIntent().getSerializableExtra("object"+i);
Even if this method works, and gets me the right results, isn't there a better/faster/cleaner solution?(I searched a lot, but haven't found a better way, maybe I don't know what exactly to look for).
Make your object serializable and put all objects in a Arraylist and send it.ArrayList itself implements serializable.
Intent newIntent = new Intent(mainA.this, secondA.class);
newIntent.putExtra("list",listOfObjets);
startActivity(newIntent);
Second Activity:
ArrayList<YourObjects> list = (ArrayList<YourObjects>)getIntent().getSerializableExtra("list");
Hi you can try this as well.. Create setter getter class which will set and get your object. next create the array list of your setter getter class.next add all objects into arraylist using set method in for loop. next add that arraylist object into bundle using setArguments.
Same you can access that bundle using getArguments that's it.
This question already has answers here:
Passing ArrayList through Intent
(6 answers)
Closed 9 years ago.
Is there a way to pass an ArrayList <ArrayList<Integer>> floors to another activity through Bundle?
Thanks
Is there a way to pass an ArrayList > floors to
another activity through Bundle?
Unfortunetly not.
If you would have ArrayList without nested it will work with putIntegerArrayList(key, value) and getIntegerArrayList(key).
But there is for sure another approach(es).I will explain you one possible way.
You can create class that will implement Serializable interface and in this class just create field and appropriate getter. I will give you basic example. Then you will pass Serializable through Activities.
public class DataHelper implements Serializable {
private ArrayList<ArrayList<Integer>> floors;
public DataHelper(ArrayList<ArrayList<Integer>> floors) {
this.floors = floors;
}
public ArrayList<ArrayList<Integer>> getList() {
return this.floors;
}
}
Save it to Bundle:
Bundle b = new Bundle();
b.putSerializable("floors", new DataHelper(floors));
and retrieve in target Activity:
getIntent().getExtras().getSerializable("floors");
To pass the arraylist from first activity to second activity.
Intent intent = new Intent(context, SecondActity.class);
intent.putIntegerArrayListExtra("arraylist",integerList); //integerList is ArrayList<Integer>
startActivity(intent);
To get the arrayList in second Activity.
ArrayList arrayList<Integer> = getIntent().getIntegerArrayListExtra("arraylist")
Read here.
If you want to pass the custom object between activities then read this thread.
I have this application with 2 classes, in the first class I define a list array wich I want to access in my second class, how do I do that? class one (with the array extends listActivity and the other class extends Activity). I don't think it's nessecary to post my code as I believe there is a quick solution to this I just don't know it. Allthough I can post it if you can't help me without seeing the actual code.
You can use a custom ApplicationContext to share global state between activities - see here for more details...
you can pass ArrayList<String> 's across activities using intentExtras.
ArrayList<String> myArrayList = new ArrayList<String>();
// populate your array -> if you want to send objects make a string form of them
Intent myIntent = new Intent(this, Activit2.class);
mtIntent.putStringArrayList("my_array_list", myArrayList);
In Activity2
Intent intent = getIntent();
List<String> stringMediaList = intent.getStringArrayListExtra("my_array_list");
for (int i = 0; i < stringMediaList.size(); i++) {
//reconstruct object if needed
}
this will work if you dont want to use parcelable