检索第二活动parcelable客体来mainActivity该调用第二个值 [英] Retrieve the value of a parcelable objet from the second activity to mainActivity that invoke the second
问题描述
我有实现Parcelable类的类。从主要活动,我传递一个parcelable对象。在第二次活动我检索客体,并使用OBJET修改数据。一切都OK。但问题是,当我需要检索从第二个活动的主要活动这一变化。我不知道我怎么能做到这一点。
在这里的主要活动电话:
公共类MainActivity延伸活动{
Tgestion Tges =新Tgestion();
按钮buttonI,buttonM,buttonB,buttonD,按钮;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
addListenerOnButton();
}公共无效addListenerOnButton(){ 最后上下文的背景下=这; buttonI =(按钮)findViewById(R.id.buttonIntroducir); buttonI.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
意向意图=新意图(背景下,IntroducirPatron.class);
intent.putExtra(com.example.sistemacontrasena.gestion,Tges);
startActivity(意向); } });
}
}
Tges是实现parcelable类的客体。
在第二个活动:
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_introducir_patron); // --- Obtenemos objeto Parcelable desde埃尔意向
this.gestion = getIntent()getParcelableExtra(com.example.sistemacontrasena.gestion);}
这之后,我设置例如this.gestion.setSecret(值)的一些变化;
当我关闭本次活动,在主要活动,我不知道我怎么可以和我说,例如值获取this.gestion.getSecret()。我该怎么做?
类parcelable是:
进口android.os.Parcel;
进口android.os.Parcelable;
公共类Tgestion实现Parcelable {私有String []秘密;
公共Tgestion(){ this.secret =新的String [2];}
/ **
*构造Tgestion段包裹。
* @参数源
* /
私人Tgestion(包裹源){
this.secret =新的String [2];
readFromParcel(源);}公共无效setSecret(字符串[] S){ 的for(int i = 0; I< this.secret.length;我++){ this.secret [I] = S [I] }}公众的String [] getSecret(){ 返回this.secret;
}@覆盖
公众诠释describeContents(){
// TODO自动生成方法存根
返回0;
}@覆盖
公共无效writeToParcel(包裹DESTINO,诠释标志){
// TODO自动生成方法存根
destino.writeStringArray(this.secret);
}私人无效readFromParcel(包裹中){ in.readStringArray(this.secret);
}
公共静态最终Parcelable.Creator< Tgestion> CREATOR =新Parcelable.Creator< Tgestion>(){ @覆盖
公共Tgestion createFromParcel(包裹源){
返回新Tgestion(源);
} @覆盖
公共Tgestion [] newArray(INT大小){
返回新Tgestion【尺寸】;
}
};}
您不能随便更改 Parcelable
,并期望第一个活动看到这些变化。 Parcelables并进行序列化和反序列化,这意味着你有一个新的副本。
您应该使用 startActivityForResult()
,的setResult()
(在第二活动)和的onActivityResult()
(在第一个)返回的数据。
请参阅取得结果从一个文档活动。
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];
}
};
}
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.
这篇关于检索第二活动parcelable客体来mainActivity该调用第二个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!