I need to understand how to use MvxCachingFragmentCompatActivity. I asked this question before the previous question , but I have an example code that is useful, but not the one I need. I need to understand how to use it.
First of all, I have one action, and all my views are fragments.
My big assumption is that using MvxCachingFragmentCompatActivity will allow me to restore the application navigation hierarchy if my activity is demolished and needs to be restored. Can someone confirm if this is correct.
If this is correct, how should I use it. for instance
- Do I need to implement the Save and Restore state in view models? Is anything else the developer needs to do?
- What does the MvxFragmentAttribute IsCacheableFragment parameter actually do with regard to fragment caching?
- What performs the action of recreating my fragment hierarchy when activity is restored?
It would be great if there was some kind of documentation.
I need to know this, as my activity breaks down and then recovers after I use another action for the camera function. When an Activity restores itself, the ViewModel for my fragments is null. I also find that Close (this) does not work in my view model. I am sure that I am not doing everything that I need to do this work, but I need to be guided by how it should be used.
Any help would be appreciated, maybe someone from the MvvmCross team. I am really stuck here. I would prefer a description of the behavior rather than pointing to a pattern, but both of them would be great.
[ Update ] Therefore, I built the debug version of the V4 and V7 MvvmCross libraries and set it to debug. As far as I can tell, as long as you add the following attributes to your fragment class, this should lead to caching of your fragments.
[MvxFragment(typeof(MainActivityViewModel), Resource.Id.contentFrame, AddToBackStack = true, IsCacheableFragment = true)] [Register("com.dummynamespace.MyFragment")]
Note that lowercase namespace is important; your class name may be mixed.
However, I still see problems after my activities are destroyed and recreated. In my case, I actually see how my activity was destroyed and recreated more than once in a row. One example is that I cannot close the view after being destroyed and recreated. This is because the code in GetFragmentInfoByTag (class MvxCachingFragmentCompatActivity) returns the wrong information needed to close the view. Similar functionality requires a ContentId from the returned IMvxCachedFragmentInfo, however this returns it as 0. Also, the AddToBackStack property is set to false. Below I have listed what is returned in the fragment information
AddToBackStack = false CacheFragment = true CachedFragment = null ContentId = 0 FragmentType = This is set to the correct fragment type Tag = This is set to the corresponding view model for the fragment
Before the action is destroyed and recreated, the fragment information is correct.
I am using MvvmCross 4.2.3. Does anyone else experience this?
Update 03/03/2017 I found out that my activity is being destroyed and recreated not because of memory, but because of the orientation of the camera. We found that this was unsuccessful when we held the camera in landscape mode.
The question that ContentId was set to 0 was caused by the fact that my application was not able to resolve the implementation of IMvxJsonConverter. This happens when the MvvmCross Json plugin is not installed. You should also add the following to your App.cs file so that it can be registered
Mvx.RegisterType<IMvxJsonConverter, MvxJsonConverter>();
If this is not done, then Try.Resolve fails, and the code that uses it is skipped. Sometimes this is done silently when he displays a magazine. It seems to me that this should probably be fatal if you expect your application to withstand activation, which will be torn down and restored.
In addition, the MvvmCross Json plugin is installed, you need to implement the save and restore state template in the save-Restore view models
Update new problem 03/08/2017 I am testing the restoration of all the views in my application. I do this by letting the orientation change, which destroys my MvxCachingFragmentCompatActivity, and then re-creates it.
When an action is destroyed, my fragment is also destroyed. At this point, I remove the review model to make sure that it will be free and not cause a memory leak.
However, I ran into a problem when OnCreate is called. It seems that he is doing two things.
- Get view model from OnCreate method MvxFragmentExtensions call in view model cache
- Then calls RestoreViewModelsFromBundle
The problem is that calling MvxFragmentExtensions OnCreate (1) calls the view model in the cache and returns a view model that has not been run, for example, the Start () function called on it, but it is used to set the DataContext.
After RestoreViewModelsFromBundle (2) is called DataContext, the event is not set again, although it passed through Constructor-> Init-> RestoreState-> Start. Therefore, I now have a view model that is not configured properly, and therefore my view does not work.
When I extracted my code to remove the view models, I got a little more, because now the cached view model installed (1) had the correct data. But I am facing other problems because it is trying to create a new view model due to the call to RestoreViewModelsFromBundle (2). As a short-term solution, in any case, I can force the view model created as part of the recovery process to be set as ViewModel
Can someone from the MvvmCross team please help with some information on what is going on here and why?