Issue
I have a Android.Support.V4.Fragment
that contains both a MapView
and a RecyclerView
. These are separate views in the Fragment
.
The app crashes when the device is rotated:
Android.OS.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
I am passing the lifecycle methods to the MapView as required by the docs:
private MapView mapView;
private RecyclerView myRecyclerView;
private RecyclerView.LayoutManager myLayoutManager;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
mapView.OnCreate(savedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
...
if (mapView != null) mapView.OnSaveInstanceState(outState);
base.OnSaveInstanceState(outState);
}
If I remove the RecyclerView
form the AXML it rotates correctly, if I include it the app crashes at mapView.OnCreate(savedInstanceState)
why is this?
Solution
For anyone that searches and has the same problem. This is the solution, It would appear to be a 2yr old bug.
I was able to get around it by saving the MapView's saved state on a separate Bundle and then adding it to the outgoing saved state bundle. Then in
onCreate()
, I would just grab the MapView's saved state from the incoming one and pass it into itsonCreate()
method, thus stopping the crashes.
In my case I implemented as so:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
Bundle mapViewSavedInstanceState = savedInstanceState != null ? savedInstanceState.GetBundle("mapViewSaveState") : null;
mapView.OnCreate(mapViewSavedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
if (mapView != null)
{
Bundle mapViewSaveState = new Bundle(outState);
outState.PutBundle("mapViewSaveState", mapViewSaveState);
mapView.OnSaveInstanceState(outState);
}
base.OnSaveInstanceState(outState);
}
Answered By - tallpaul
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.