Monika Kumari Monika Kumari - 3 years ago 76
Android Question

I want to send integer,String,Object,double,Bitmap,HashMap from one activity to another in android

I am begnning in android , I want to send String,Object,double,ArrayList from one activity to another . I already done with integer but when i want to send other data like object and ArrayList i am facing problem.

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);


ReceiverActivity

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);

Answer Source

Passing double data:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found

Passing String data:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");

or

Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");

Passing ArrayList data :

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");

Passing Object data :

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");

Note : Keep in mind your custom Class must implement the Serializable interface.

Passing HashMap data :

SenderActivity

HashMap<String, String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);

ReceiverActivity

Intent mIntent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");

Passing Bitmap data :

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);

ReceiverActivity

Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download