onPreated()和onStop()之间的活动是onCreated()

时间:2014-10-07 09:29:45

标签: c# android xamarin

我无法在我的应用中可靠地退出活动。当我按下设备上的“后退”-Button或按下按钮时调用finish()时,活动显然是随机重启。

示例活动:

[Activity(Label = "abc", ParentActivity = typeof(MainActivity))]
public class ImpressumActivity : Activity
{
    private static int _instancecounter;
    private int _thisinstance;
    protected override void OnCreate(Bundle bundle)
    {
        _thisinstance = _instancecounter++;
        Console.WriteLine("Creating instance {0}", _thisinstance);
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Impressum);
        Console.WriteLine("Created instance {0}", _thisinstance);
    }

    public override void Finish()
    {
        Console.WriteLine("Finishing instance {0}", _thisinstance);
        base.Finish();
        Console.WriteLine("Finished instance {0}", _thisinstance);
    }

    protected override void OnStop()
    {
        Console.WriteLine("Stopping instance {0}", _thisinstance);
        base.OnStop();
        Console.WriteLine("Stopped instance {0}", _thisinstance);
    }

    protected override void OnPause()
    {
        Console.WriteLine("Pausing instance {0}", _thisinstance);
        base.OnPause();
        Console.WriteLine("Paused instance {0}", _thisinstance);
    }

    protected override void OnDestroy()
    {
        Console.WriteLine("Destroying instance {0}", _thisinstance);
        base.OnDestroy();
        Console.WriteLine("Destroyed instance {0}", _thisinstance);
    }
}

现在,我在11:20:50的MainActivity中通过impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); };开始活动,然后在11:20:53按下后退按钮。活动“仍然”在屏幕上;实际上它会被重新创建,正如我从输出中看到的那样:

10-07 11:20:50.255 I/mono-stdout( 6149): Creating instance 0
10-07 11:20:50.275 I/mono-stdout( 6149): Created instance 0
10-07 11:20:50.275 D/Activity( 6149): setTransGradationMode to true
10-07 11:20:53.535 I/mono-stdout( 6149): Finishing instance 0
10-07 11:20:53.555 I/mono-stdout( 6149): Finished instance 0
10-07 11:20:53.565 I/mono-stdout( 6149): Pausing instance 0
10-07 11:20:53.585 I/mono-stdout( 6149): Paused instance 0
10-07 11:20:53.605 I/mono-stdout( 6149): Creating instance 1
10-07 11:20:53.625 I/mono-stdout( 6149): Created instance 1
10-07 11:20:53.625 D/Activity( 6149): setTransGradationMode to true
10-07 11:20:54.015 I/mono-stdout( 6149): Stopping instance 0
10-07 11:20:54.015 I/mono-stdout( 6149): Stopped instance 0
10-07 11:20:54.015 I/mono-stdout( 6149): Destroying instance 0
10-07 11:20:54.015 I/mono-stdout( 6149): Destroyed instance 0

我在其他活动中有相同的行为。我尝试NoHistory=true,这有帮助,但打破其他事情(我确实想在更复杂的活动中使用通话记录),有和没有ParentActivity = ...,但我被卡住了。

更新:这是一个MainActivity,我可以用它来重现()行为: ():请注意,我描述的行为并不总是出现。当我第二次进入ImpressumActivity时似乎大多数,或者更具体地说,我需要在第n次进入活动时按“返回”n次(尽管它已被销毁)每一次!)

[Activity(Label = "@string/MainHL", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
    }

    protected override void OnResume()
    {
        base.OnResume();
        var impressumButton = FindViewById<Button>(Resource.Id.impressum);
        impressumButton.Click += delegate { StartActivity(typeof (ImpressumActivity)); };
    }
}

1 个答案:

答案 0 :(得分:1)

确实,这就是问题所在。每次OnResume发生时,您都会点击事件但不会释放它。它会导致在单击时创建许多新活动实例。而不是OnCreate中的点击事件,你会没事的。