简体   繁体   English

活动是在onPause()和onStop()之间的onCreated()

[英]Activity is onCreated() between onPause() and onStop()

I cannot reliably exit Activities in my app. 我无法在我的应用中可靠地退出“活动”。 When I press the "back"-Button on the device or I call finish() from a button click, the activity is - apparently randomly - restarted. 当我按下设备上的“后退”按钮或通过单击按钮调用finish() ,活动-显然是随机的-重新开始。

Example Activity: 活动示例:

[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);
    }
}

Now, I start the activiy via impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); }; 现在,我通过impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); };开始活动。 impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); }; impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); }; on 11:20:50 in my MainActivity, then I press the back-Button at 11:20:53. 在MainActivity中的11:20:50上,然后在11:20:53按下后退按钮。 The Activity "remains" onscreen; 屏幕上的“活动”仍然存在; actually it gets recreated, as I can see from the output: 从输出中可以看到,实际上它是重新创建的:

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

I have the same behaviour in other Activities. 我在其他活动中也有相同的行为。 I tried NoHistory=true , which helps, but breaks other things (I do want a call history in the more complicated Activities), with and without ParentActivity = ... , but I am stuck. 我尝试了NoHistory=true ,这有帮助,但在有和没有ParentActivity = ...情况下,都打破了其他事情(我确实希望在更复杂的Activity中获得通话记录),但是我陷入了困境。

Update: This is a MainActivity with which I can reproduce( ) the behaviour: ( ): Please note that the behaviour I described does not always appear. 更新:这是一个MainActivity,可用来再现( )行为:( ):请注意,我描述的行为并不总是出现。 It seems to appear mostly when I enter the ImpressumActivity for the second time, or, more specifically, I need to press "Back" n times the nth time I entered the Activity (notwithstanding that it had been destroyed each time!) 当我第二次输入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)); };
    }
}

Indeed, that's the problem. 确实,这就是问题所在。 Each time OnResume happens you wire click event but you don't release it. 每次发生OnResume时,您都会关联单击事件,但不会释放它。 It results in as many new activity instances being created upon click. 单击后将导致创建尽可能多的新活动实例。 Wire click event in OnCreate instead and you'll be fine. 在OnCreate中关联单击事件,您会好起来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM