Search code examples
androidandroid-intentparcelable

Why getting wrong value from parcelable when passed with intent to other activity?


I am trying to pass data from adapter to activity through bundle.

I am checking a checkbox

            checkboxSelectSubCategory.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    productModelsAL.get(getAdapterPosition()).setCategorySelected(true);
                }else {
                    productModelsAL.get(getAdapterPosition()).setCategorySelected(false);
                }
            }
        });

and setting data correctly in the object as I have debugged:

   Intent intent = new Intent(mContext, SelectProductActivity.class);
   Bundle bundle = new Bundle();
   bundle.putParcelable("productModel", productModelsAL.get(getAdapterPosition()));
   intent.putExtras(bundle);
   mContext.startActivity(intent);

But in activity I am not receiving the boolean value which was passed. in activitie's onCreate:

    Intent intent = getIntent();
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            subCategory = (SubCategory) bundle.getParcelable("productModel");
        }
    }
    allProductsSelected = subCategory.isCategorySelected();

Why I am not getting the value I am passing??


Solution

  • Actually, my Model class was incorrect.

    I had model class n all implemented correctly first.

    But for the extra functionality I included the checkbox in the model and created setter getters. But didn't updated the required methods for Parcelable.

    Solution:

    I deleted the three auto generated methods for Parcelable.

    1. Constructor
    2. Creator
    3. writeToParcel

    and recreated them with Alt+Enter.

    That's it.

    It was bit ignorant and I wasted lot of time with it. Just thought it might help somebody.