user8160152
user8160152

Reputation:

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); 

Upvotes: 0

Views: 536

Answers (5)

Athar Iqbal
Athar Iqbal

Reputation: 458

Intent mIntent = new Intent(MainActivity.this, AboutActivity.class);
ArrayList<String> mArray = new ArrayList<>();
mArray.add("Mon");
mArray.add("Tue");
mIntent.putStringArrayListExtra("Array", mArray);
startActivity(mIntent);

// In AboutActivity.java recieved data
ArrayList<String> mArray= getIntent().getExtras().getStringArrayList("Array");
Toast.makeText(AboutActivity.this, ""+mArray, Toast.LENGTH_LONG).show();

Upvotes: 0

Anmol317
Anmol317

Reputation: 1376

You can make a bean of the data you want to send like this

public class YourBean implements Serializable {

String yourString;
Object yourObject;

double yourDouble;
ArrayList<String> yourList;

public String getYourString() {
    return yourString;
}

public void setYourString(String yourString) {
    this.yourString = yourString;
}

public Object getYourObject() {
    return yourObject;
}

public void setYourObject(Object yourObject) {
    this.yourObject = yourObject;
}

public double getYourDouble() {
    return yourDouble;
}

public void setYourDouble(double yourDouble) {
    this.yourDouble = yourDouble;
}

public ArrayList<String> getYourList() {
    return yourList;
}

public void setYourList(ArrayList<String> yourList) {
    this.yourList = yourList;
}
}

then when you want to pass data in intent do like this

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
    YourBean yourBean = new YourBean();
    yourBean.setYourString("your string");
    yourBean.setYourDouble(Your double);
    yourBean.setYourObject(Your Object);
    yourBean.setYourList(array list);
    intent.putExtra("bean",yourBean);
    startActivity(intent);

then you can get it like this in your SecondActivity like this

YourBean yourBean1 = (YourBean) getIntent().getSerializableExtra("bean");

Upvotes: 1

Yogesh Mane
Yogesh Mane

Reputation: 611

You can send object one activity to another by using Serializable class

check following example: http://hmkcode.com/android-passing-java-object-another-activity/

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/

you can send like:

    Intent intent = new Intent(mcontext, activity.class);
    intent .putExtra("Key",product);
    startActivity(intent );

and get value like:

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();`enter code here`
    product = (ClassName) bundle.getSerializable("Key");

Upvotes: 2

Abhishek Kumar
Abhishek Kumar

Reputation: 1265

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");

Upvotes: 4

JakSok
JakSok

Reputation: 124

You can use intents. In a intent you can put all sort of data, String, int, whatever you want.

In your case, in activity1, before going to activity2, you will store a String message this way :

Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);

In activity2, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Then you can set the text in the TextView or where you want

Hope this helps you !

Upvotes: 0

Related Questions