-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android ViewModel Life Cycle #1
Comments
@cosminstirbu The way i solved this issue was: public string PutParameter(object parameter)
{
lock (_parametersByKey)
{
var guid = Guid.NewGuid().ToString();
_parametersByKey.Add(guid, parameter);
return guid;
}
}
public object GetAndRemoveParameter(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
lock (_parametersByKey)
{
if (_parametersByKey.ContainsKey(key))
{
var param = _parametersByKey[key];
_parametersByKey.Remove(key);
return param;
}
return null;
}
} then I changed my Activity to: private const string ViewModelKey = "ViewModel";
protected override void OnCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
_vm = GlobalNavigation.GetAndRemoveParameter<DetailViewModel>(savedInstanceState.GetString(ViewModelKey));
else
_vm = GlobalNavigation.GetAndRemoveParameter<DetailViewModel>(Intent);
...
}
protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutString(ViewModelKey, GlobalNavigation.PutParameter(_vm));
base.OnSaveInstanceState(outState);
} This works like a charm without having to change anything major |
Thanks for the reply. I'm not a big fan of a static / global navigation service. It should get released soon and it should be available via nuget. In the meanwhile I am using my own binding library to expose it C#. Regards, |
Hi Laurent,
I've been following your work on MVVM Light for a while and I'd like to thank you for your contribution to the UWP / Xamarin community.
I had a closer look at how you're using the NavigationService on Android and it's interesting that you're passing the ViewModels to the destination Activity and retrieving them using the GetAndRemoveParameter method (and it seems that you're following this pattern for iOS and UWP as well).
Since Android has a bit of complexity surrounding its Activities and Fragments lifecycle, how would you suggest handling that so that the ViewModel is kept "alive" during lifecycle events (such as configuration changes)?
Would you maybe suggest going with the Lifecycle Helpers that the Android team is introducing recently via - https://developer.android.com/topic/libraries/architecture/index.html ?
Thank you,
Cosmin
The text was updated successfully, but these errors were encountered: