简体   繁体   中英

How to pass an ArrayList to the StartActivityForResult activity

I need to from Activity A start an Activity B for result. I need to pass a String ArrayList from Activity A to Activity B first. I thought that this code would work but it crashes the app with a message that the list was not passed:

Activity A:

Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);

Activity B:

Bundle gotList = getIntent().getExtras();
ArrayList<String> listB = gotList.getStringArrayList("list");

Replace:

Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);

with:

Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
intent.putStringArrayListExtra("list",listA);
startActivityForResult(intent,2);

The Bundle that is available as a parameter on startActivityForResult() is not how you pass Intent extras.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM