将具有多个部分的对象从一个活动传递到另一个活动

时间:2014-10-15 06:00:48

标签: android object android-intent android-activity

嗨我有一个类似于人员详细信息的要求,因为我有多个详细信息,如个人,教育,工作,如在第一个活动中输入个人详细信息,然后进行教育,然后就像那个一个接一个的工作,我怎么能保持所有细节在单个对象中并通过应用程序请帮助我。

enter image description here

2 个答案:

答案 0 :(得分:0)

请注意,您需要在Intent中捆绑对象,该对象必须实现Parcelable接口。以下是Parcelable documentation ...

的常见实现
public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

完成后,只需在Intent中传递对象......

Intent i = new Intent(getApplicationContext(), ACTIVITY_TO_START.class);
i.putExtra("extra_key", new MyParcelable());
startActivity(i);

检索起始活动的对象......

Bundle extras = getIntent().getExtras();

if(extras != null && extras.containsKey("extra_key"))
{
    MyParcelable p = (MyParecelable)extras.getParcelable("extra_key");
}

答案 1 :(得分:0)

简单解决方案:对此

使用意图

<强> Personal.class

public class Personal extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) {

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

        Intent i = new Intent(getApplicationContext(), Educational.class);
        i.putExtra("personal_details",<-get data from object->);
        startActivity(i);
    } 
}

<强> Educational.class

public class Personal extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) {

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

        String personal_details; 

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        personal_details= extras.getString("personal_details");
        } 

        Intent i = new Intent(getApplicationContext(), educational.class);
        i.putExtra("personal_details",personal_details);
        i.putExtra("educational_details",<-get data from object->);
        startActivity(i);
    } 
}

<强> Job.class

public class Personal extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) {

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

        String personal_details,educational_details; 

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        personal_details= extras.getString("personal_details");
        educational_details= extras.getString("educational_details");
        } 

        Intent i = new Intent(getApplicationContext(), FinalResult.class);
        i.putExtra("personal_details",personal_details);
        i.putExtra("educational_details",educational_details);
        i.putExtra("job_details",<-get data from object->);
        startActivity(i);
    } 
}

<强> FinalResult.class

public class Personal extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal);
        String personal_details,educational_details,job_details; 

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        personal_details= extras.getString("personal_details");
        educational_details= extras.getString("educational_details");
        job_details= extras.getString("job_details");
        } 
    } 
}

{EDIT-1}

{EDIT-2}

嗯,如果我理解你的问题 ::你已经连接到互联网(Wifi,有线等等),基本上你正试图在没有网络连接时显示对话框! ....你可以借助Broadcast receivers ...

试试这个:: 设置广播接收器在没有网络连接时激活意图.... 编写代码以捕获该意图并弹出dialog ....在此对话框中,为用户提供重新连接连接的选项!