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.
- putBooleanArray
- putByteArray
- putCharArray
- putCharSequenceArray
- putDoubleArray
- putFloatArray
- putIntArray
- putLongArray
- putParcelableArray
- putShortArray
- 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
精彩评论