wetwork
wetwork

Reputation: 113

How to pass an activity intent to another class?

I have an arraylist. I get this arraylist from another activity with using Bundle like that :

Bundle name = getIntent().getExtras();
ArrayList<String> namevalue = name.getStringArrayList("name"); 

I want to use same arraylist in a different class. But i can't get with using getIntent() method becoz of my class is not an activity. Is there a way to pass this arraylist?

Upvotes: 0

Views: 1020

Answers (2)

kgiannakakis
kgiannakakis

Reputation: 104198

You can create a setter in your class:

public class MyClass {

  List<String> names;

  public void setNames(List<String> names) {
    this.names = names;
  }
}

Then call setNames from your Activity.

Upvotes: 1

xil3
xil3

Reputation: 16449

You could pass it to the class as a parameter (via constructor or through another method):

Bundle name = getIntent().getExtras();
ArrayList<String> namevalue = name.getStringArrayList("name"); 

YourClass yc = new YourClass(namevalue);

Upvotes: 3

Related Questions