My ViewModel implements serializable and i have defined on button click methods as below..
public class FragmentHomeViewModel
extends BaseObservable
implements Serializable {
private Rerofit retrofit;
private HomeScreen activity;
public FragmentHomeViewModel(HomeScreen activity) {
this.activity = activity;
retrofit = MyGlobal.getMyGlobale(activity).getNetComponent().retrofit();
}
//
//....code
public void onClickCuisine(View view){
Intent intent = new Intent(view.getContext(), SelectCuisineActivity.class);
(view.getContext()).startActivity(intent);
Log.e("is clicked"," may be");
}
}
and here is my xml class attached with this view model
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data class="Item4DataMindingCuisine">
<import type="com.aman.camellia.kniterider.utils.Constrants" />
<variable
name="index"
type="int"/>
<variable
name="viewModel"
type="com.aman.camellia.kniterider.viewmodel.FragmentHomeViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dim6dp"
android:layout_marginLeft="@dimen/dim10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="View All"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/text16sp"
android:onClick="@{viewModel::onClickCuisine}"
app:fontText="@{Constrants.BOLD_FONT}"
/>
</RelativeLayout>
</layout>
Now at the time on textview click i am getting
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.aman.camellia.kniterider.viewmodel.FragmentHomeViewModel)
Caused by: java.io.NotSerializableException: retrofit2.Retrofit$1
Any idea why this error is occured.i think reason of this error may be retrofit which i used(actually i used dagger for this).
Ok finally i got the answere . The reason of this problem was, my viewmodel implement Serializable and retrofit instance is not serialized so either you need to serialized retrofit or make retrofit variable as transient by which it will not be serialized. And after that my viewmodel will be...
public class FragmentHomeViewModel
extends BaseObservable
implements Serializable {
transient Rerofit retrofit;
private HomeScreen activity;
public FragmentHomeViewModel(HomeScreen activity) {
this.activity = activity;
retrofit = MyGlobal.getMyGlobale(activity).getNetComponent().retrofit();
}
//
//....code
public void onClickCuisine(View view){
Intent intent = new Intent(view.getContext(), SelectCuisineActivity.class);
(view.getContext()).startActivity(intent);
Log.e("is clicked"," may be");
}
}
And doing this another activity start succesfully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With