나는 여기 mvvmlight.net에서 Android Xamarin 예제를 따라 갔다. 두 작업 모두 ActivityBase를 확장합니다. 적절한 키를 사용하여 _nav.NavigateTo를 호출하면 다른 활동이 열리지 만 OnCreate는 매번 호출되며 savedInstanceState는 항상 null입니다. OnDestroy() 및 OnSaveInstanceState()는 호출되지 않습니다. NavigateTo가 마지막 활동을 파괴하지 않고 새로운 활동을 생성하는 것 같습니다. 이 올바른지?MVVM Light의 NavigateTo가 새 활동을 재사용하지 않고 만드는 이유는 무엇입니까?
Android 활동주기를 살펴볼 때 OnCreate는 한 번만 호출해야합니다. 매번 다른 활동을 다시 만들기보다는 다시 시작할 수 있습니까?
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>
GC에서 수집 한 이전 활동이 있습니까? OnDestroy()는 내가 볼 수있는 한 호출되지 않습니다. 어떻게 청소해야합니까? – eirik
나는 MVVMLight에 익숙하지 않다 ... 나는 그것들을 수집 할 수 있도록하기 위해 활동에 대한 참조를 유지하지 않을 것을 기대한다. 그런 다음 기본 GC가 수집 할 때 메모리 압력에 따라 실제로 예측할 수는 없습니다. 개발자 목적으로 Android를 떠나려면 Android를 강제 종료 할 수 있습니다 (개발자 설정에서 Nexus 5X에서 '활동 계속하지 않음'). – GGirard