How to take dice from a Fragment and play in an Activity?

Asked

Viewed 469 times

0

I have a class in java that takes all the information and assembles the entire structure of my program. Well, as I do to pick up an Edittext that is in my Ragment and play its content inside the other Activity.

Example:

My Fragment calls a layout that has a comment field. This Fragment is called in another Activity. When saving the data I need to take the data typed by the user in this Fragment Edittext and save. Well I tried to do this, but it always gives error of null Pointer excpetion... I also tried to create an internet and could not. If it was with Eventbus how would it be? I’ve tried searching all over the internet and found nothing to solve the problem

  • Dude, take a look at this: http://stackoverflow.com/questions/25134645/get-edittext-value-from-fragment .... i will not try to post because I am without how to try to reproduce and also I do not have your code

  • Friend, check the following topic: http://answall.com/a/129380/46186

1 answer

1

One of the ways to pass data between activities is by creating interfaces and implementing, 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();
    }
}

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

Take this data in another Activity

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

More information: http://www.survivingwithandroid.com/2015/05/android-parcelable-tutorial-list-class.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.