1

I know this has been asked so many times, but the answers never seem to apply.

activity 1:

public void buttonPress(View view){
    Intent i = new Intent(this,ProfilePage.class);  
    i.putExtra("USER_DETAILS", UD);
    startActivity(i);
}

activity 2:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_page);
    try{
        UserDetails UD = (UserDetails)this.getIntent().getExtras().getParcelable("USER_DETAILS");
        ((TextView)findViewById(R.id.First)).setText(UD.getFirst_name());
        ((TextView)findViewById(R.id.Last)).setText(UD.getLast_name());
    }catch (Exception e) {
        Log.e("GETTING EXTRAS", e.toString());
    }
}

"UD" is parcelable as I have it returning correctly elsewhere. this.getIntent().getExtras().getParcelable("USER_DETAILS") is simply returning null. I continue to run into this problem, how to I fix it or what am I simply not getting?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Joseph Dailey
  • 4,735
  • 2
  • 15
  • 18

5 Answers5

1

Try

UserDetails UD = (UserDetails) getIntent().getParcelableExtra("USER_DETAILS");
ps2010
  • 861
  • 1
  • 7
  • 13
  • well, I figured out my problem, (I had a field named UD too)<--idiot but I'm wondering if there's a difference between the too methods of getting extras? – Joseph Dailey May 10 '13 at 03:43
  • 1
    According to Android Intent documentation, getExtra() returns you a map of all the extras you added with putExtra(). For specifically getting your parcelable object you have to use getParcelableExtra(). – ps2010 May 11 '13 at 03:06
0

You could do something like this by making UserDetails implement Serializable:

Intent i = new Intent(this,ProfilePage.class);  
i.putExtra("USER_DETAILS", UD);

and then retrieve the object like this:

UserDetails UD = (UserDetails)this.getIntent().getSerializableExtra("USER_DETAILS");

EDIT: In performance matters Serializable is slower than Parcelable, Check this out. Anyway, I posted this as a workaround to your problem.

Community
  • 1
  • 1
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
  • Wouldn't I still have to `implement` Serializable anyways. I also heard it was slower. Please set me strait if I'm wrong! – Joseph Dailey May 10 '13 at 03:45
0

Maybe this can help you with Parcelable

If you are sending a non-primitive type data/Object to another activity through the intent you have to either Serialize or implement Parcelable for that object. The preferred technique is Parcelable since it doesn't impact the performance.

A lots of people will tell you that Serialization is very slow and inefficient which is correct. But, one thing you never want to do as a computer programmer is take any comment about performance as an absolute.

Ask yourself if serialization is slowing your program down. Do you notice when it goes from activity to activity? Do you notice when it saves/loads? If not, it's fine. You won't get a smaller footprint when you go to a lot of manual serialization code, so there is no advantage there. So what if it is 100 times slower than an alternative if 100 times slower means 10ms instead of 0.1ms? You're not going to see either, so who cares? And, why would anyone invest massive effort into writing manual serialization when it won't make any perceptible difference in performance?

Hein
  • 2,675
  • 22
  • 32
0

I just developed my first Parcelable class which implements the interface Parcelable. I am really happy because it works correctly. Maybe my solution can help anyone:

public class DealCategory implements Parcelable {

private int categoryID;
private String categoryName;
private List<DealCategory> listaCategoriasSeleccionadas = new ArrayList<DealCategory>();

/**
 * GET/SET 
 */



//-----------------------------------------------------------|
//-----------------------------------------------------------|
//------------------- METHODS FOR PARCELABLE ----------------|
//-----------------------------------------------------------|
//-----------------------------------------------------------|

/*
 * (non-Javadoc)
 * @see android.os.Parcelable#describeContents()
 * Implementacion de los metodos de la Interfaz Parcelable
 */
@Override
public int describeContents() {
    return hashCode();
}

/*
 * (non-Javadoc)
 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
 * IMPORTANT
 *  We have to use the same order both TO WRITE and TO READ 
 */
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(categoryID);
    dest.writeString(categoryName);
    dest.writeTypedList(listaCategoriasSeleccionadas);  
}


/*
 * (non-Javadoc)
 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
 * IMPORTANT
 *  We have to use the same order both TO WRITE and TO READ
 *  
 * We reconstruct the object reading from the Parcel data
 */ 
public DealCategory(Parcel p) {  
    categoryID = p.readInt();    
    categoryName = p.readString();   
    p.readTypedList(listaCategoriasSeleccionadas, DealCategory.CREATOR);     
}


/*
 * (non-Javadoc)
 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
 * We need to add a Creator
 */ 
public static final Parcelable.Creator<DealCategory> CREATOR = new Parcelable.Creator<DealCategory>() {

    @Override    
    public DealCategory createFromParcel(Parcel parcel) { 
        return new DealCategory(parcel);
    }

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

}

I send(write) the Object Parcelable "DealCategory" from Activity A to Activity B

protected void returnParams(DealCategory dc) {
      Intent intent = new Intent();
      intent.putExtra("Category", dc);
      setResult(REQUEST_CODE_LISTA_DEALS, intent);
      finish()
}

I receive(read) the Object Parcelable "DealCategory" in Activity B from Activity A

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle b = data.getExtras();             
    DealCategory dc = (DealCategory) b.getParcelable("Category");

I check if I receive corrected values. I temporaly show them in log

for (int i = 0; i < dc.getListaCategorias().size(); i++) {
            Log.d("Selected Category", "ID: " +  dc.getListaCategorias().get(i).getCategoryID() + " -- NAME:" + dc.getListaCategorias().get(i).getCategoryName());
            lR += dc.getListaCategorias().get(i).getCategoryName() +", ";
        }

} //Close onActivityResult
user2356533
  • 51
  • 1
  • 4
0
UserDetails UD

was declared as both a field and a local variable and I didn't notice,

I guess a good byproduct of this question is...

getIntent().getParcelableExtra("USER_DETAILS");
getIntent().getExtras().getParcelable("USER_DETAILS");

...work differently and one needs to stay consistent in approach.

Joseph Dailey
  • 4,735
  • 2
  • 15
  • 18