简体   繁体   中英

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.

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)); }; on 11:20:50 in my MainActivity, then I press the back-Button at 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.

Update: This is a MainActivity with which I can reproduce( ) the behaviour: ( ): Please note that the behaviour I described does not always appear. 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!)

[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. It results in as many new activity instances being created upon click. Wire click event in OnCreate instead and you'll be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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