James James - 3 months ago 5x
Java Question

What is the best way to pass a set to another activity. Java Android SDK

I have a set which contains a list of strings:

public Set<String> favs = new HashSet<>();

However when I start another activity 'Favorites' I want to pass this list over to my 'Favorties' class, currently I have:

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
} else if (id == R.id.exit_the_app) {
return true;
} else if (id == R.id.favorites) {
Intent startfavs = (new Intent(Insulter.this, Favorites.class));
return true;
return super.onOptionsItemSelected(item);

I want to pass this set in 'favorites' and eventually display it in a list view after changing it to a list. What would be the best way of going about doing this?

(Note: My second activity is all set up in the manifold and runs fine)


In your First Activity called Insulter,add this,

Intent startfavs = (new Intent(Insulter.this, Favorites.class));
String[] objects = new String[set.size()];
final ArrayList<String> list = new ArrayList<String>(Arrays.asList(objects));

In your second Activity called Favorites,to get the param,

ArrayList<String> favs = getIntent().getStringArrayListExtra("favs");