与线程使用Android的黑白屏幕旋转? [英] Screen Rotation with Threading using Mono Android?

查看:140
本文介绍了与线程使用Android的黑白屏幕旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CommonsWare电子书的Andr​​oid 3.6版第270,他讨论如何处理与旋​​转螺纹。他提供了在那里你创建一个内部静态类来保存你的状态,称之为分离工作流程,然后屏幕旋转过程中再次连接的解决方案。

In CommonsWare eBook Android v3.6 pg 270, he discusses handling threads with rotation. He offers a solution where you create an inner static class to hold your state, call detach in the workflow and then attach again during screen rotation.

问题是,每一次旋转会破坏活动并重新创建它在Android中,因此,当你回来你的线程可以引用破坏活动给你访问收集设置对象异常。

The problem is that each rotation will destroy the Activity and recreate it in Android, thus when you come back your thread may reference the destroyed activity giving you an exception for accessing an object set for collection.

在我的Andr​​oid单尝试这样无法得到它的工作,我得到了一个例外,每一次。我的问题,希望乔纳森窥探者读这是,我怎么可以在Android的单这项工作?我在没有结果的论坛上问这个的两倍。所以我就抽空计算器。我想张贴actualy code,但我不想违反CommonsWare许可。所以,请看看书中的例子。

I tried this in Mono Android and was unable to get it to work, I got an exception every single time. My question, hopefully Jonathan Pryer reads this is, how can I make this work in Mono Android? I have asked this twice on the forums with no results. So I am taking it to StackOverflow. I wanted to post the actualy code but I didn't want to violate CommonsWare licensing. So please take a look at the example in the book.

推荐答案

什么是例外?什么是亚行logcat 输出?

What was the exception? What's the adb logcat output?

C#的相当的是,同样的,但是,不同于Java源代码。例如,下面是Android的样本项目的旋转感知版本的缺省单的:

The C# equivalent is the-same-but-different from the Java source. For example, here's the "rotation-aware" version of the default Mono for Android sample project:

[Activity (Label = "Scratch.PreserveCount", MainLauncher = true)]
public class Activity1 : Activity
{
    CountInfo Info;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);

        button.Click += delegate {
            SetButtonCount (button, Info.Count++);
        };

        Info = (CountInfo) LastNonConfigurationInstance;
        if (Info == null) {
            Info = new CountInfo {
                Count = 1,
            };
        } else {
            SetButtonCount (button, Info.Count);
        }
    }

    void SetButtonCount (Button button, int count)
    {
        button.Text = string.Format ("{0} clicks!", Info.Count);
    }

    public override Java.Lang.Object OnRetainNonConfigurationInstance ()
    {
        return Info;
    }

    class CountInfo : Java.Lang.Object {
        public int Count;
    }
}

这是相同的基本方法为Java示例:活动。 OnRetainNonConfigurationInstance()方法被调用Android的活动被布置之前,所以我们从这个方法返回我们的国家。该 Activity.LastNonConfigurationInstance 属性将返回第一次它被调用,否则将返回从 OnRetainNonConfigurationInstance返回的最后的值()。唯一缺少的一块拼图是国家的对象( CountInfo 点击这里)必须的的java.lang.Object 为我们传递的实例的Java

It's the same basic approach as the Java sample: the Activity.OnRetainNonConfigurationInstance() method is invoked by Android before the Activity is disposed, so we return our "state" from that method. The Activity.LastNonConfigurationInstance property will return null the first time it's invoked, otherwise it will return the last value returned from OnRetainNonConfigurationInstance(). The only missing piece of the puzzle is that the "state" object (CountInfo here) must inherit from Java.Lang.Object as we're passing the instance to Java.

这篇关于与线程使用Android的黑白屏幕旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆