开发者

How to Pass Array to another activity?

开发者 https://www.devze.com 2023-04-01 05:35 出处:网络
I know that we can able to pass the any object with its value to another activity with the method putExtra() and getExtra methods.

I know that we can able to pass the any object with its value to another activity with the method putExtra() and getExtra methods. but now I want to know whether is it possible to pass the array to the another Activity or not? Or if it is then let me know how I can pass the array to the another Activity ? Thank开发者_运维知识库s.


Bundle b = new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

And for receiveing

Bundle b = this.getIntent().getExtras();
String[] array=b.getStringArray(key);


The Bundle class has many putXxxxArray() methods.

  1. putBooleanArray
  2. putByteArray
  3. putCharArray
  4. putCharSequenceArray
  5. putDoubleArray
  6. putFloatArray
  7. putIntArray
  8. putLongArray
  9. putParcelableArray
  10. putShortArray
  11. putStringArray

If you are looking to pass some sort of Object, you should look at the Parcelable interface, as your Objects will need to implement it.


Firstly, you should know two issues:

  • in order to send sth via an intent it must be parcelable
  • even if an object is pacelable, there is limitation to the amount of data you can send via an intent.

A possible approach could be to have a static structure where you could store your data and pass with the intent only an index to that data. Using this index the new activity could get access to those data. Hope this helps.


Please see this question. Basically:

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

To retrieve:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);


 Intent intent = new Intent();
 Bundle bundle = new Bundle();
 bundle.putSerializable("bundle",Parceble Object);
 intent.putExtra(String key, String[] values);
 intent.putExtras(bundle);

And for different types of array look here.

Thnx

0

精彩评论

暂无评论...
验证码 换一张
取 消