0

could someone please answer my question. in my activity class.java, I have an intent and i want to send an object using intent. when I searched for it, the results were I have to implement parcelable to the class "object" that I want to send it. I did that but the thing is I want to put two objects to be sent to main2activity.java, when I tried to do that, my app crashed, when I debugged it said that main2activity has much intent? so my question is how can I send two objects using put extra, and get them in the other java using getintent.getparcelableextra()?

mainactivity.java

clickedplace is an object of class called Place

Intent myintent = new Intent(getApplicationContext(), localpopup.class);
                    myintent.putExtra("localprice",clickedplace.getTicketId().getLocalPrice());
                    myintent.putExtra("placeobject", clickedplace.getId());

main2activity.java localpriceplace of type double getpressplace of type Place

localpriceplace= getIntent().getParcelableExtra("localprice");
        getpressedplace= (Place) getIntent().getParcelableExtra("placeobject");

4 Answers4

0

You should be able to do this by creating a Bundle and putting each Intent into it with a String key. In "main2activity" you should retrieve them by name.

But the bundle into a new Intent and send that to main2activity

Otherwise, if you are putting data into each Intent and then sending the Intent directly to main2activity, you will need to implement "onNewIntent" in main2activity to get the new data.

Jim
  • 10,172
  • 1
  • 27
  • 36
0

Hi Please follow below link which is useful to you and solved your problem.

http://www.vogella.com/tutorials/AndroidParcelable/article.html

Hope above link helps you.

If you want to auto generate those parcelable model class then follow below link which saves your time.

https://github.com/mcharmas/android-parcelable-intellij-plugin

Please check below code:

1) You need to create your model class as a parcelable follow by above link.

2) To pass data to other activity:

 intent.putExtra("student", new Student("1","Mike","6") // which is your model class object passing);

3)GetData into Other Activity Using intent:

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");
// after you get all data from student.

Hope you understand.

Jyubin Patel
  • 1,373
  • 7
  • 17
  • thank you i think i have already opened this before, my problem isnot this. my problem is that after i put two different keys and values and tried to get them in two different getintent.getparceableextra, my application crashes, because a class cannot have two getintent. i hope you understand my explanation. i have put code in order to visualize my problem. – Mariam Mohie Sleem Jan 02 '18 at 11:44
  • please tell me what is getLocalPrice().? it's string or Model class object. – Jyubin Patel Jan 02 '18 at 11:47
0

ActivityB.java

public class ActivityB {

   private static final String ARG_DATA1 = "Data1";
   private static final String ARG_DATA2 = "Data2";

   public static startActivityB (Context context, Data1 data1, Data2 data2) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ARG_DATA1, data1);
        bundle.putParcelable(ARG_DATA2, data2);
        Intent intent = new Intent(context, ActivityB.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        Data1 data1 = bundle.getParcelable(ARG_DATA1);
        Data2 data2 = bundle.getParcelable(ARG_DATA2);
    }
}

Data1.java

public static class Data1 implements Parcelable {
    public static final Creator<Data1> CREATOR = new Creator<Data1>() {
        @Override
        public Data1 createFromParcel(Parcel in) {
            return new Data1(in);
        }

        @Override
        public Data1[] newArray(int size) {
            return new Data1[size];
        }
    };

    protected Data1(Parcel in) {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    }
}

and Data2.java should also implement Parcelable.

And lastly, to start ActivityB from ActivityA,

Data1 data1;
Data2 data2;

ActivityB.startActivityB(ActivityA.this, data1, data2);
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

Try this implements Parcelable to your Place Model

Intent myintent = new Intent(getApplicationContext(), localpopup.class);                  
myintent.putExtra("placeModel",clickedplac);

Receive

placeModel= getIntent().getParcelableExtra("placeModel");
String price=placeModel.getTicketId().getLocalPrice();
String Id=placeModel.getId()
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39