Actually the work-around I presented here does not seem to work after all.
It does fix the bug, but Android doesn't always reset statics when the app
exits normally, so this can cause it to exit the app right after you launch
it.  Instead, it's necessary to keep track of the number of instances that
exist in memory:

 

 

    [Activity(MainLauncher = true)]

    public class Activity1 : Activity

    {

        private static int _numInstances = 0;

 

        protected override void OnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

 

            _numInstances++;

 

            if (_numInstances > 1)

                this.Finish();

            else

            {

                var btn = new Button(this);

                btn.Text = "One";

                btn.Click += (s, e) =>
this.StartActivity(typeof(Activity2));

 

                SetContentView(btn);

            }

        }

 

        protected override void OnDestroy()

        {

            base.OnDestroy();

 

            _numInstances--;

        }

    }

 

 

From: Randy Ficker [mailto:randyfic...@gmail.com] 
Sent: Thursday, May 10, 2012 2:49 PM
To: 'Discussions related to Mono for Android'
Subject: RE: [mono-android] Odd behavior when restarting app

 

Hey Jon,

 

Thanks for the reply, but I think this issue is actually a new bug and not
the same bug as what you linked.  In case you didn't realize, both of the
links you gave me were written by me.

 

The differences are:

 

1.       That old bug did repro 100% of the time for me in 1.9.2, regardless
of the circumstances.  In this new bug, it works sometimes, but fails in
specific circumstances.  I do believe that old bug was fixed in 4.0.1 as the
bug describes, as everything worked great for me in 4.0.1.

2.       Under the old bug, the app was simply reverting to the first
activity.  That is, in the sample code below, Activity2 would simply be
getting deleted.    In this new bug, a new copy of the first activity is
created and pushed onto the stack.

 

The old bug was just a nuisance as it meant if the app was interrupted by a
phone call or something the user just lost their state and had to start
over.   However, this new bug is far worse, since it leaves the app sort of
in a corrupt state with activities all out of order.

 

With the steps outlined, I can repro this 100% of the time.  If you try my
steps and it doesn't repro for you, let me know and we can try to find the
difference between our environments.

 

I wish I could go back to an earlier version of MfA, but 4.0.6 has the fix
to allow ICS devices to use OpenTK apps, which is essential to me.  This
restarting bug is the only thing preventing me from releasing my app to ICS
users and they're growing restless J

 

The old bug was also impossible to work-around, since the activities were
actually getting deleted.  This new bug can actually be fixed if you
manually try to detect MfA creating a second instance of your main activity:

 

    [Activity(MainLauncher = true)]

    public class Activity1 : Activity

    {

        private static bool _exists = false;

 

        protected override void OnCreate(Bundle bundle)

        {

            Log.Info("", "Activity1.OnCreate");

            base.OnCreate(bundle);

 

            if (_exists)

                this.Finish();

            else

            {

                _exists = true;

 

                var btn = new Button(this);

                btn.Text = "One";

                btn.Click += (s, e) =>
this.StartActivity(typeof(Activity2));

 

                SetContentView(btn);

            }

        }

    }

 

This is obviously pretty hacky, but it works.  With this code, the new copy
of Activity1 gets closed as soon as it's opened and the app works as
expected.  I'm still debating whether or not I should release my app this
way. 

 

 

-----Original Message-----
From: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Jonathan Pryor
Sent: Thursday, May 10, 2012 1:08 PM
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Odd behavior when restarting app

 

On May 10, 2012, at 5:15 AM, Randy Ficker wrote:

> After re-launching the app, I would expect to see Activity2 presented to
the user.  Instead, it shows Activity1.  But the problem is much worse than
just seeing the wrong activity, since if the user presses Back they actually
go back to Activity2!  Going back again goes back to another copy of
Activity1.

 

This is known, but I haven't been able to figure out why it happens yet:

 

 
<http://lists.ximian.com/pipermail/monodroid/2011-November/006985.html>
http://lists.ximian.com/pipermail/monodroid/2011-November/006985.html

                 <http://bugzilla.xamarin.com/show_bug.cgi?id=1919>
http://bugzilla.xamarin.com/show_bug.cgi?id=1919

 

Like you, I've had no luck getting a sample that repros 100% of the time,
which makes it rather difficult to figure out what's going wrong...

 

Sorry,

- Jon

 

_______________________________________________

Monodroid mailing list

 <mailto:Monodroid@lists.ximian.com> Monodroid@lists.ximian.com

 

UNSUBSCRIBE INFORMATION:

 <http://lists.ximian.com/mailman/listinfo/monodroid>
http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to