2

I'm looking for a way to pass, via bundle, an

ArrayList<Integer[]> 

object to a Fragment (after activity is already created, so i can't use intent). By looking to android api, no method seems to do what i'm looking for. How can i do?

3
  • 1
    ArrayList of object. Shows us your code Commented Feb 17, 2014 at 14:46
  • Sorry, i want to pass an ArrayList of integer array Commented Feb 17, 2014 at 14:50
  • i would send using shared preferences and remove them ,when they are no longer needed Commented Feb 17, 2014 at 15:00

4 Answers 4

2

Sending activity:

final Intent intent = new Intent(this, SecondActivity.class);
Bundle extraBundle = new Bundle();

extraBundle.putIntegerArrayList("arraylist", [put array list here]);

intent.putExtras(extraBundle);
intent.setComponent(new ComponentName("com.myapp", "com.myapp.SecondActivity"));

startActivity(intent);

Receiving activity's onCreate():

final Intent intent = getIntent();
final Bundle extraBundle = intent.getExtras();

ArrayList<Integer> myIntegerArrayList = extraBundle.getIntegerArrayList("arraylist");

You can change "arraylist" to what you want in the setter and getter method calls, they just need to be the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Activity is already started, i need to pass that ArrayList to a fragment
@giozh you need to edit your question with more details and be specific
0

I used Gson, convert to JSONArray and send via Bundle. But it may affects performance.

In First Activity

Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myArrayList", new Gson().toJson(myArrayList);
startActivity(activity);

In other activity..

Sting myArrayList;
Bundle extras = getIntent().getExtras();
if (extras != null) {
   myArrayList= extras.getString("myArrayList");

   Type listOfInteger = new TypeToken<List<Integer>>(){}.getType();
   String s = new Gson().toJson(myArrayList, listOfInteger);
   List<Integer> myList = new Gson().fromJson(s, listOfInteger); 
}

Comments

0
int[] intarray = new int[] {4,5,6,7,8};

Bundle bundle = new Bundle();
bundle.putIntArray("integerarray", intarray);

Or an ArrayList with int arrays?

For that i think you should use Parcable. Or you could try to send the individual intArrays via the bundle and put them together in the receiving Activity. Like this:

int[] intarray1 = new int[] {4,5,6,7,8};
int[] intarray2 = new int[] {4,5,6,7,8};
int[] intarray3 = new int[] {4,5,6,7,8};

Bundle bundle = new Bundle();
bundle.putIntArray("INT_ARRAY1", intarray1);
bundle.putIntArray("INT_ARRAY2", intarray2);
bundle.putIntArray("INT_ARRAY3", intarray3);

Intent intent = new Intent(this,NewActivity.class)
intent.putExtras(bundle);
startActivity(intent)

Then in NewActivity.java you should create an array and get the bundle and fill the array with the received arrays.

1 Comment

that's not the problem you could do that dynamically but is this the result you want? send your individual arrays and put them in an ArrayList at the other Activity
0

you can pass the Array List of integers by using the method putIntegerArrayList

Here is the code snippet

 ArrayList<Integer> values=new ArrayList<>();
            values.add(1);
            values.add(60);
            values.add(75);
            values.add(120);
 Bundle extras = new Bundle();
 extras.putIntegerArrayList(EXTRA_KEY,values); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.