Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid passing an arraylist back to parent activity
    primarykey
    data
    text
    <p>I've been searching for a simple example of this with no luck.</p> <p>In my android application I have two activities: 1. The main activity which is launched at startup 2. A second activity which is launched by pressing a button on the main activty.</p> <p>When the second activity is finished (by pressing a button) I want it to send back an ArrayList of type MyObject to the main activity and close itself, which the main activity can then do whatever with it. How would I go about achieving this? I have been trying a few things but it is crashing my application when I start the second activity.</p> <p>When the user presses button to launch second activity:</p> <pre><code> Intent i = new Intent(MainActivity.this, secondactivity.class); startActivityForResult(i, 1); </code></pre> <p>The array which is bundled back after pressing a button on the second activity:</p> <pre><code>Intent intent= getIntent(); Bundle b = new Bundle(); b.putParcelableArrayList("myarraylist", mylist); intent.putExtras(b); setResult(RESULT_OK, intent); finish(); </code></pre> <p>And finally a listener on the main activity (although I'm not sure of 100% when this code launches...)</p> <pre><code> protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==RESULT_OK &amp;&amp; requestCode==1){ Bundle extras = data.getExtras(); final ArrayList&lt;MyObject&gt; mylist = extras.getParcelableArrayList("myarraylist"); Toast.makeText(MainActivity.this, mylist.get(0).getName(), Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Any ideas where I am going wrong? The onActivityResult() seems to be crashing my application.</p> <p>EDIT: Class with parcelable methods:</p> <pre><code>import android.os.Parcel; import android.os.Parcelable; public class Plan implements Parcelable{ private String name; private String id; public Plan(){ } public Plan createFromParcel(Parcel in) { Plan plan = new Plan(); plan.setId(in.readString()); plan.setName(in.readString()); return plan; } public Plan(String name, String id){ this.name = name; this.id = id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getId(){ return id; } public void setId(String id){ this.id = id; } public String toString(){ return "Plan ID: " + id + " Plan Name: " + name; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); } public static final Parcelable.Creator&lt;Plan&gt; CREATOR = new Parcelable.Creator&lt;Plan&gt;() { public Plan createFromParcel(Parcel in) { return new Plan(); } @Override public Plan[] newArray(int size) { // TODO Auto-generated method stub return new Plan[size]; } }; } </code></pre> <p>After the second activity finished, onactivityresult is called, but nothing displays inside the toast, its blank. any ideas? I'm guessing my class is still messed up...</p> <p>EDIT: got it to work</p> <p>I had the method that Peter supplied in the wrong place. It should be inside creator, like this:</p> <pre><code>public static final Parcelable.Creator&lt;Plan&gt; CREATOR = new Parcelable.Creator&lt;Plan&gt;() { public Plan createFromParcel(Parcel in) { Plan plan = new Plan(); plan.setId(in.readString()); plan.setName(in.readString()); return plan; } </code></pre> <p>and not out on its own.</p> <p>Many thanks to Peter! Hope this helps someone else.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload