【问题标题】:Retrieve the value of a parcelable objet from the second activity to mainActivity that invoke the second从第二个活动中检索可打包对象的值到调用第二个的 mainActivity
【发布时间】:2014-06-14 17:50:34
【问题描述】:

我有一个实现 Parcelable 类的类。从主要活动中,我传递了一个可包裹的对象。在第二个活动中,我检索对象并使用对象更改数据。一切正常。但问题是当我需要检索从第二个活动到主要活动的更改时。我不知道我该怎么做。

这里是主活动中的调用:

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 是一个实现 parcelable 类的对象。 在第二个活动中:

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

}

在此之后,我设置了一些更改,例如 this.gestion.setSecret(value); 例如,当我关闭第二个活动时,在主要活动中,我不知道如何使用我输入的值检索 this.gestion.getSecret()。我该怎么做?

类parcelable是:

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];
    }
};

}

【问题讨论】:

    标签: android android-intent parcelable


    【解决方案1】:

    您不能只更改Parcelable 并期望第一个活动看到这些更改。 Parcelables 被序列化和反序列化,这意味着你有一个新副本。

    您应该使用startActivityForResult()setResult()(在第二个活动中)和onActivityResult()(在第一个活动中)返回数据。

    请参阅文档中的 Getting a Result from an Activity

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多