I'm looking for a way to assign value to my viewModel class in a way that it persists across an activity's fragments. Assigning value as mViewModel = serializedPOJO; doesn't work and the only other way I know is calling mViewModel.setFieldValue(serializedPOJO.getFieldValue()); for every field in the class and I believe there should be a simpler way than this.

Here's my code:


public class POJOViewModel extends ViewModel {
    private String id;
    private String clientName;
    private String date;

    public POJOViewModel() {

    public String getClientName() {
        return clientName;

    public void setClientName(String clientName) {
        this.clientName = clientName;
    //And similar getter setters


public class Activity extends AppCompatActivity {
    private POJOViewModel mViewModel, serializedJSON;

    protected void onCreate(final Bundle savedInstanceState) {
        mViewModel = ViewModelProviders.of(this).get(POJOViewModel.class);
        ActivityArgs args = ActivityArgs.fromBundle(getIntent().getExtras()); //Contains data sent from previous activity
        mViewModel = args.getFormData(); //Assignment. However, mViewModel holds data, it doesn't persist when accessed from another fragment.

0 Answers