How to get data from a Fragment and play in an Activity?

0

I have a class in java that takes all the information and builds the entire structure of my program. Well, how do I get an EditText that is in my fragment and throw its contents into the other activity.

Example:

My fragment calls a layout that has a comment field. This fragment is called in another Activity. At the time of saving the data I need to get the data entered by the user in this EditText of Fragment and save. Well I've tried to do this, but it always gives null pointer excpetion error ... Also I tried to create an intent and I could not. If it were with EventBus what would it be like? I've already tried searching the entire internet and found nothing that solved the problem.

    
asked by anonymous 03.07.2015 / 22:12

1 answer

1

One of the ways to pass data between activities is to create interfaces and implement, or via intent that makes more sense in your case.

Create a class that will be your converter

public class ContactInfo implements Parcelable {

    private String name;
    private String surname;
    private int idx;

    // get and set method

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(surname);
        dest.writeInt(idx);
    }

    // Creator
    public static final Parcelable.Creator<contactinfo> CREATOR
            = new Parcelable.Creator<contactinfo>() {
        public ContactInfo createFromParcel(Parcel in) {
            return new ContactInfo(in);
        }

        public ContactInfo[] newArray(int size) {
            return new ContactInfo[size];
        }
    };

    // "De-parcel object
    public ContactInfo(Parcel in) {
        name = in.readString();
        surname = in.readString();
        idx = in.readInt();
    }
}

PAs your activity data

Intent i = new Intent(MainActivity.this, ActivityB.class);
// Contact Info
ContactInfo ci = createContact("Francesco", "Surviving with android", 1);
i.putExtra("contact", ci);

Get this data in another activity

Intent i  = getIntent();
ContactInfo ci = i.getExtras().getParcelable("contact");

More information: link

    
23.02.2016 / 20:27