Ich habe das Android Xamarin Beispiel von hier mvvmlight.net gefolgt. Beide Aktivitäten erweitern ActivityBase. Wenn ich _nav.NavigateTo mit den richtigen Schlüsseln aufrufen, wird die andere Aktivität geöffnet, aber OnCreate wird jedes Mal aufgerufen und savedInstanceState ist immer Null. OnDestroy() und OnSaveInstanceState() werden nie aufgerufen. Es scheint, dass NavigateTo eine neue Aktivität erstellt, ohne die letzte zu zerstören. Ist das richtig?Warum erstellt MVVM Light NavigateTo neue Aktivitäten, anstatt sie wiederzuverwenden?
Wenn Sie den Android Activity-Lebenszyklus betrachten, sollte OnCreate nur einmal aufgerufen werden. Wie kann ich einfach die andere Aktivität fortsetzen, anstatt sie jedes Mal neu zu erstellen?
LoginActivity:
[Activity(Label = "Login", MainLauncher = true, Icon = null)]
public class LoginActivity : ActivityBase
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.LoginLayout);
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
var nav = new NavigationService();
nav.Configure(Constants.Keys.Main, typeof(MainActivity));
nav.Configure(Constants.Keys.Login, typeof(LoginActivity));
nav.Configure(Constants.Keys.Live, typeof(LiveActivity));
...
SimpleIoc.Default.Register<INavigationService>(() => nav);
LoginLayout.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp">
... some buttons etc
</LinearLayout>
MainActivity:
public class MainActivity : ActivityBase
{
private INavigationService _nav;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
_nav = ServiceLocator.Current.GetInstance<INavigationService>();
}
private void Settings_Click(object sender, EventArgs e)
{
_nav.NavigateTo(Constants.Keys.Settings, null);
}
main.axml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/MainLayout">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content"
android:layout_below="@id/toolbar">
<Button
android:text="Live"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonLive"
android:layout_marginTop="20dp"/>
<Button
android:text="Alerts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonAlerts" />
<Button
android:text="Settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonSettings" />
</LinearLayout>
</RelativeLayout>
Sind also die alten Aktivitäten von GC gesammelt? OnDestroy() wird nie aufgerufen, so weit ich sehen kann. Wie soll ich sie aufräumen? – eirik
Ich bin nicht wirklich vertraut mit MVVMLight ... Ich erwarte, dass es keine Referenzen zu den Aktivitäten hält, damit sie gesammelt werden können. Dann, wenn der native GC sie sammeln wird, ist er nicht wirklich vorhersehbar, abhängig vom Speicherdruck. Für Dev-Zwecke können Sie Android zwingen, Aktivitäten zu zerstören, sobald Sie eine verlassen (es ist in den Entwicklereinstellungen, "Aktivitäten in einem Nexus 5X nicht behalten") – GGirard