Skip to content
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

Open
cosminstirbu opened this issue Oct 8, 2017 · 2 comments
Open

Android ViewModel Life Cycle #1

cosminstirbu opened this issue Oct 8, 2017 · 2 comments

Comments

@cosminstirbu
Copy link

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

@BickelLukas
Copy link

@cosminstirbu The way i solved this issue was:
I added these 2 Methods to the implementation of the NavigationService:

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

@cosminstirbu
Copy link
Author

@BickelLukas

Thanks for the reply.

I'm not a big fan of a static / global navigation service.
I've ended up creating a PR on AndroidSupportComponents that also includes an example of how you can leverage the Android Architecture Components to use the same View Model during configuration changes.

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,
Cosmin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants