Retrieve the value of a parcelable objet from the

2019-09-10 04:05发布

I have a class that implements the Parcelable class. From main activity, I pass an parcelable object. At the second activity I retrieve the objet and use the objet to change data. All is ok. But the problem is when I need to retrieve this changes from second activity to the main activity. I don't know how could I do it.

Here the call in the main activity:

public class MainActivity extends Activity {
Tgestion Tges= new Tgestion();
Button buttonI,buttonM,buttonB,buttonD,buttonS; 

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

public void addListenerOnButton() {

    final Context context = this;

    buttonI = (Button) findViewById(R.id.buttonIntroducir);

    buttonI.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) { 
            Intent intent = new Intent(context, IntroducirPatron.class);
            intent.putExtra("com.example.sistemacontrasena.gestion", Tges);
            startActivity(intent);               

        }

    });
}
}

Tges is an objet that implements the parcelable class. In the second activity:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_introducir_patron);

    // --- Obtenemos objeto Parcelable desde el Intent
    this.gestion=getIntent().getParcelableExtra("com.example.sistemacontrasena.gestion");

}

After this I set some changes for example this.gestion.setSecret(value); When I close this second activity, in the main activity I don't know how can I retrieve this.gestion.getSecret() with the value that I put, for example. How can I do that?

The class parcelable is:

import android.os.Parcel;
import android.os.Parcelable;


public class Tgestion implements Parcelable{

private String[] secret;


public Tgestion(){

    this.secret=new String[2];

}


/**
 * Constructor Tgestion para parcel.
 * @param source
 */
private Tgestion(Parcel source) {
    this.secret=new String[2];
    readFromParcel(source);

}

public void setSecret(String[] s){

    for (int i=0;i<this.secret.length;i++){

        this.secret[i]=s[i];

    }

}

public String[] getSecret(){

    return this.secret;
}   

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel destino, int flags) {
    // TODO Auto-generated method stub
    destino.writeStringArray(this.secret);
}

private void readFromParcel(Parcel in){

    in.readStringArray(this.secret);
}


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

    @Override
    public Tgestion createFromParcel(Parcel source) {
        return new Tgestion(source);
    }

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

}

1条回答
放我归山
2楼-- · 2019-09-10 04:44

You cannot just change the Parcelable and expect the first activity to see these changes. Parcelables are serialized and deserialized, which means that you have a new copy.

You should use startActivityForResult(), setResult() (in the second activity) and onActivityResult() (in the first one) to return data.

See Getting a Result from an Activity in the docs.

查看更多
登录 后发表回答