-4

First of all i wanna say that how to pass Arraylist between activities. I wanna know whether whole arraylist is passed on using intent or just its base adress is transferred. (as in C) Moreover if whole arraylist is passed then is there any mechanism to just transfer its base as in c so multiple activities can use a single arraylist n not copy of same arraylist. Note Before downvoating this question i qanna say that i had visited many ither arraylist related questions on stackoverflow but none had explained my problem.

2 Answers2

0
ArrayList<String> arr = new ArrayList<>();
        arr.add("Hello");

        Intent intent = new Intent(MainActivity.this, ToActivity.class);
        intent.putExtra("array_list", arr);
        startActivity(intent);

In ToActivity activity:

Bundle bundle = getIntent().getExtras();
         if (bundle != null) {
            ArrayList<String> arr = b.getStringArrayList("array_list");
        }
Rajan Singh
  • 309
  • 2
  • 7
0

Data that can be passed through intents are Serializable, Parcelable or primitive type. ArrayList is already serializable so it gets serialized when passed across activities. If you want to use same Arraylist in different activities you can create singleton to access the same ArrayList from different points

mguest
  • 121
  • 2