Resolved
Please, I need some help!
Android Situation:
MSVisualStudio 2015 + Xamarin + Mvvmcross 4.4
One Activity and Several Fragments. Using default presenter.
1- View (Fragment) with MvxRecyclerView, a row is selected for editing.
2- Show new view (Fragment) with a MvxSpinner, and change its value (A) to another one (B). Remember: A & B are objects.
<Mvx.MvxSpinner
android:id="@+id/spinner_academic_start_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxDropDownItemTemplate="@layout/spinner_basic"
local:MvxItemTemplate="@layout/item_spinner_basic"
local:MvxBind="ItemsSource AcademicYearWeeksCollection; SelectedItem AcademicStartWeek; HandleItemSelected SelectAcademicStartWeekCommand" />
and there is a ViewModel with
public IList<AcademicYearWeeks> AcademicYearWeeksCollection { get { return _academicYearWeeksCollection; } }
where AcademicYearWeeks is a class with properties and so on.
3- Cancel edition and return to View with MvxRecyclerView.
4- Repeat click for edit same row.
5- Oooppps! Spinner shows B not A. Why????
Other considerations:
Fragment with spinner always is called using
ShowViewModel<CourseViewModel>(
new
{
courseId = item.CourseId
});
and its ViewModel associated
public void Init(int courseId)
{
_courseId = courseId;
}
public override void Start()
{
base.Start();
LoadAcademicYearWeeksCollection();
if (_courseId == 0)
NewCourse();
else
LoadCourse();
}
and is closed using
Close(this)
I'll be very greatful!
Thanks.
When you create a fragment using MvxFragment
attribute, by default the IsCacheableFragment
is set to true. Therefore, as the fragment and it ViewModel are being cached and you get the behaviour you are experiencing when returning to the page for a second time.
Ideally you should just be able to set the IsCacheableFragment
to false
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame,
IsCacheableFragment = false)]
However, the behaviour of this feature is, I believe, undesired. As according to GitHub issue it sets your ViewModel to null.
Another approach you can try it to force the fragment and ViewModel pair to refresh themselves. In your fragments parent activity override ShowFragment
and set the forceAddToBackStack
to true for your ViewModel with the spinner on it
protected override void ShowFragment(string tag, int contentId, Bundle bundle, bool forceAddToBackStack = false, bool forceReplaceFragment = false)
{
if (tag.Equals(typeof(<<Your View Model Type>>).FullName))
forceReplaceFragment = true;
base.ShowFragment(tag, contentId, bundle, forceAddToBackStack, forceReplaceFragment);
}