Android: Passing Objects between Activities

most of the times you would need to pass a POJO object through some activities to make sure you have everything related to a sample topic in one place as an OOP best practice. so to pass an object between some activities you need to do something like this …

inside the first activity you need to create your POJO and then add that to the nextScreen, then you need to make sure your POJO can be serialized, and then in nextScreen Activity read that POJO back and after unmarshaling back to the object shape read its content.
Step #1:

for example, I have a component that enables the app to start reading the QR code like this method.

public class Home extends AppCompatActivity implements View.OnClickListener {

private static MyObj obj1 = new MyObj();

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

}


public void onActivityResult(int requestCode, int resultCode, Intent intent) {

IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

if (scanningResult != null) {

String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();

Intent nextScreen = null;
if (isInFieldPath) {
nextScreen = new Intent(getApplicationContext(), FieldOptionMenu.class);

} else {
nextScreen = new Intent(getApplicationContext(), LabOptionMenu.class);
}

nextScreen.putExtra("format", scanFormat);
nextScreen.putExtra("content", scanContent);

polioData.setQrCodeContent(scanContent);
polioData.setQrCodeFormat(scanFormat);
nextScreen.putExtra("MySimpleObj",obj1);

startActivity(nextScreen);


} else {
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}

}

Step #2:

you need to implement below items inside your POJO to make that serializable in android.


public class MyObject implements Parcelable {

private String something = "Navidamo Navidam";


public MyObject() {
super();
}

protected MyObject(Parcel in) {
something = in.readString();

}

public String getUsername() {
return something;
}

public void setUsername(String something) {
this.something = something;
}



// writing and serialization process

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(something);

}

//reading back to object from serilization process - unmarshaling data
public static final Creator<MyObject> CREATOR = new Creator<MyObject>() {
@Override
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}

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


}

Step #3:

and inside the destination activity simply use the below lines wherever you can get the intent.

MyObject obj = (MyObject) getIntent().getParcelableExtra("MySimpleObj");
Toast.makeText(getApplicationContext(),
obj.getSomething() + "wiht out bundle", Toast.LENGTH_LONG).show();

//            one way of getting data from Extra
//            Bundle bundle = getIntent().getExtras();
//            obj = (MyObject) bundle.getParcelable("MySimpleObj");
//            Toast.makeText(getApplicationContext(),
//                    obj.getSomething() + "with bundle", Toast.LENGTH_LONG).show();

 

Advertisements