Add Items To An Arraylist That Has Been Passed Between Activities
Solution 1:
First thing, you should use Parcelable
, it is more efficient in this case.
Have a look at this link for the "why":
https://medium.com/@theblackcat102/passing-object-between-activity-using-gson-7dfa11d74e06
Second, I'd do it like this:
publicclassActivityOneextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Intentintent=newIntent(this, ActivityTwo.class);
ArrayList<Person> strings = newArrayList<Person>(Arrays.asList(newPerson("Bob"),newPerson("Dude")));
Bundlebundle=newBundle();
bundle.putParcelableArrayList(ActivityTwo.PARCELABLE_KEY,strings);
intent.putExtras(bundle);
startActivity(intent);
}
}
publicclassActivityTwoextendsActivity {
publicstaticfinalStringPARCELABLE_KEY="array_key";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
if (getIntent() != null) {
ArrayList<Person> persons = getIntent().getParcelableArrayListExtra(PARCELABLE_KEY);
Log.d("test", persons.toString());
}
}
}
publicclassPersonimplementsParcelable {
private String name;
public String getName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicPerson() {
}
publicPerson(String name) {
this.name = name;
}
@OverridepublicintdescribeContents() {
return0;
}
@OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
}
publicstaticfinal Parcelable.Creator<Person> CREATOR = newParcelable.Creator<Person>() {
public Person createFromParcel(Parcel source) {
returnnewPerson(source);
}
public Person[] newArray(int size) {
returnnewPerson[size];
}
};
privatePerson(Parcel in) {
this.name = in.readString();
}
}
Also, do not be afraid of Parcelables! And as a good developer, you should be lazy, then use a Parcelable generator such as: http://www.parcelabler.com/
Just copy your POJO class(es) and generate (an Android Studio plugin also exists).
Finally, don't forget that you cannot pass unlimited data between 2 activities. You could also consider putting your data into a database.
Solution 2:
I put together a quick example of passing the array list to other activities and adding items to it.
publicclassFirstActivityextendsActivity {
publicstaticfinalStringEXTRA_FIRST_ARRAY="ExtraFirstArray";
privatestaticfinalStringTAG= FirstActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> firstArrayList = newArrayList<String>();
firstArrayList.add("First Activity Test1");
firstArrayList.add("First Activity Test2");
firstArrayList.add("First Activity Test3");
Log.d(TAG, "First Array List: ");
for (String value : firstArrayList) {
Log.d(TAG, "Value: " + value);
}
Intentintent=newIntent(this, SecondActivity.class);
intent.putStringArrayListExtra(EXTRA_FIRST_ARRAY, firstArrayList);
startActivity(intent);
}
}
publicclassSecondActivityextendsActivity {
publicstaticfinalStringEXTRA_SECOND_ARRAY="ExtraSecondArray";
privatestaticfinalStringTAG= SecondActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getExtras() != null) {
final ArrayList<String> firstArrayList = getIntent().getStringArrayListExtra(FirstActivity.EXTRA_FIRST_ARRAY);
ArrayList<String> secondArrayList = newArrayList<String>();
secondArrayList.addAll(firstArrayList);
secondArrayList.add("Second Activity Test1");
secondArrayList.add("Second Activity Test2");
secondArrayList.add("Second Activity Test3");
Log.d(TAG, "Second Array List: ");
for (String value : secondArrayList) {
Log.d(TAG, "Value: " + value);
}
Intentintent=newIntent(this, ThirdActivity.class);
intent.putStringArrayListExtra(EXTRA_SECOND_ARRAY, secondArrayList);
startActivity(intent);
}
}
}
publicclassThirdActivityextendsActivity {
privatestaticfinalStringTAG= ThirdActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getExtras() != null) {
final ArrayList<String> secondArrayList = getIntent().getStringArrayListExtra(SecondActivity.EXTRA_SECOND_ARRAY);
ArrayList<String> thirdArrayList = newArrayList<String>();
thirdArrayList.addAll(secondArrayList);
thirdArrayList.add("Third Activity Test1");
thirdArrayList.add("Third Activity Test2");
thirdArrayList.add("Third Activity Test3");
Log.d(TAG, "Third Array List: ");
for (String value : thirdArrayList) {
Log.d(TAG, "Value: " + value);
}
}
}
}
Solution 3:
If you have some data that you want to share among activities i would personally suggest that u keep them as a field on the application object so u would need to extend the application class.
publicclassMyApplication extend Application{
privateList<SomeData> data;
@overritepublicvoidonCreate(){
super.onCreate();
data = newArrayList<SomeData>();
}
publicList<SomeData> getData(){
return data
}
}
In the manifest file you would have to configure ur application class in the application xml-tag
<application
android:name:"package.name.MyApplication">
</application>
Finally in ur activities you can access them by calling:
((MyApplication)getApplication()).getData();
In this way you dont consume resources on serializing/deserializing and you have access globally in the application context. Just pay attention to not overdo this as those data will occupy the memory independently from any activity. but if intend to persist some data which u will frequently access from different this is a optimized solution compare to serializing or passing through Parcelable interface.
Solution 4:
You can use also custom intent :
publicclassSharedIntentextendsIntent {
privateList list;
publicvoidputArrayList(List list){
this.list = list;
}
publicListgetList(){
return list;
}
}
Send :
publicclassSenderActivityextendsActivity {
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedIntentintent=newSharedIntent();
intent.putArrayList(newArrayList<String>());
}
}
Retrieve :
publicclassGettingActivityextendsActivity {
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent() instanceofSharedIntent) {
SharedIntent intent = (SharedIntent) getIntent();
ArrayList<String> list = (ArrayList<String>) intent.getList();
}
}
}
Post a Comment for "Add Items To An Arraylist That Has Been Passed Between Activities"