1

When I put an object into a bundle so I can transfer it to another Activity, changing the value of the attribute IdEscritorio occurs. In my LoginActivity IdEscritorio the value of = 1, when I get in my Main Activity is it with the value of 7.

LogCat enter image description here

LoginActivity Method

private void IniciarMainActivity(Usuario usuarioAutenticado)
{
    //Iniciar Activity Main
    Intent i = new Intent(LoginActivity.this, MainActivity.class);
    Bundle b = new Bundle();
    b.putParcelable("usuarioAutenticado", usuarioAutenticado);

    Log.d(TAG, "IdEscritorio: " +  String.valueOf(usuarioAutenticado.getIdEscritorio()));

    i.putExtras(b);
    startActivity(i);
}

MainActivity onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Widgets
    InicializeComponents();

    //Listeners Events
    Listeners();

    //Recuperar os valores de usuario autenticado enviado da activity Login
    _usuarioAutenticado = new Usuario();
    Bundle bundle = getIntent().getExtras();        
    if(bundle != null)
        _usuarioAutenticado = bundle.getParcelable("usuarioAutenticado");

    Log.d(TAG, "IdEscritorio: " +  String.valueOf(_usuarioAutenticado.getIdEscritorio()));
}

Class Usuario

public class Usuario implements Parcelable {

    private int Id;
    private int IdEscritorio;
    private String Nome;
    private String Login;
    private String Senha;   

    /**
     * Construtores
     */
    public Usuario()
    {
        super();
    }

    public Usuario(Parcel parcel)
    {
        this();
        this.Id = parcel.readInt();
        this.IdEscritorio = parcel.readInt();
        this.Nome = parcel.readString();
        this.Login = parcel.readString();
        this.Senha = parcel.readString();
    }   

    /**
     * Propriedades
     */
    public void setId(int id) {
        Id = id;
    }

    public int getId() {
        return Id;
    }

    public int getIdEscritorio() {
        return IdEscritorio;
    }

    public void setIdEscritorio(int idEscritorio) {
        IdEscritorio = idEscritorio;
    }

    public String getNome() {
        return Nome;
    }

    public void setNome(String nome) {
        Nome = nome;
    }

    public String getLogin() {
        return Login;
    }

    public void setLogin(String login) {
        Login = login;
    }

    public String getSenha() {
        return Senha;
    }

    public void setSenha(String senha) {
        Senha = senha;
    }

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

    @Override
    public void writeToParcel(Parcel out, int flags) 
    {
        out.writeInt(getId());
        out.writeString(getNome());
    }


    public static final Parcelable.Creator<Usuario> CREATOR = new Parcelable.Creator<Usuario>(){

        @Override   
        public Usuario createFromParcel(Parcel in){ 
                return new Usuario(in);
            }
        @Override
        public Usuario[] newArray(int size) {
            return new Usuario[size];
            }
    };
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Renan Barbosa
  • 1,046
  • 3
  • 11
  • 31
  • Can you make it like: `if(bundle != null) { _usuarioAutenticado = bundle.getParcelable("usuarioAutenticado"); Log.d(TAG, "IdEscritorio: " + String.valueOf(_usuarioAutenticado.getIdEscritorio())); }` and check the result again? – dragi Oct 21 '14 at 13:15
  • Same result in the log, the value remains changed. – Renan Barbosa Oct 21 '14 at 13:44

1 Answers1

0

The problem is with your Parcelable class. In your writeToParcel class, you are writing only Id and Nome to the Parcel.

@Override
public void writeToParcel(Parcel out, int flags) 
{
    out.writeInt(getId());
    out.writeString(getNome());
}

However, you are reading all 5 fields when creating the object in Usuario(Parcel parcel). For this to work correctly, change your writeToParcel method to:

public void writeToParcel(Parcel out, int flags)
{
    out.writeInt(getId());
    out.writeInt(getIdEscritorio());
    out.writeString(getNome());
    out.writeString(getLogin());
    out.writeString(getSenha());
}  
AesSedai101
  • 1,502
  • 2
  • 23
  • 37