在 xamarin、monodroid 中旋转后防止重新加载活动 [英] Prevent activity from reload after rotation in xamarin, monodroid

查看:18
本文介绍了在 xamarin、monodroid 中旋转后防止重新加载活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的...所以我的问题是防止在方向改变后重新加载活动.基本上,我所做的是:

Ok... So my problem is to prevent from activity to reload after orientation is changed. Basically, what I did is this:

[Activity(Label = "migs", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]

这很好用,直到我将目标 API"更改为 14.如果我将其更改回 12,则一切正常,但在 14,活动正在重新启动(OnCreate 方法在轮换后触发).所以......你会问为什么我需要Target API"14?- 简单!因为在我的应用程序中,我正在播放视频,为此我需要真正的全屏".14 个以下的所有 API 都添加了设置"(三个点)按钮.如果是 HTC,它的按钮又大又丑,我无法摆脱.

This is worked fine, until I changed "Target API" to 14. If I'm changing it back to 12, then everything is working, but on 14, activity is being restarted (OnCreate method is fires after rotation). So... You'll ask why do I need "Target API" 14? - Easy! Because in my app, I'm playing video, and for that I need "true full screen". All API's below 14 adding "Settings" (three dots) button. In case of HTC, it's big and ugly button, that I was unable to get rid of.

如果您知道如何执行两者之一(去掉 API 12 中的设置"按钮,或在 API 14 中更改方向后防止重新加载活动),我将非常感谢您的帮助.

If you know how to do one of the two (Get rid of the "settings" button in API 12, or prevent activity from reload after orientation changed in API 14), I'll be very thank full for your help.

推荐答案

好的...我终于解决了!:)保存活动状态而不是阻止活动重新加载,乍一看似乎有点棘手,但实际上很容易,而且它是此类情况的最佳解决方案.就我而言,我有一个 ListView,它从 Internet 上填充存储在自定义列表适配器中的项目.如果设备方向发生变化,Activity 会重新加载,ListView 也会重新加载,我将丢失所有数据.我需要做的就是覆盖 OnRetainNonConfigurationInstance 方法.这是如何做到这一点的快速示例.
首先,我们需要一个类,它可以处理我们所有的东西.

这是我们需要保存的所有内容的包装器:

Ok... At last I solved it! :) Saving activity state instead of preventing activity from reload, from first sight can seem to be a little tricky, but in fact is really easy and it's the best solution for situations like this. In my case, I had a ListView, that populates from the internet with items, that stored in custom list adapter. If device orientation was changed, the activity was reloaded, so does the ListView, and I was loosing all the data. All I needed to do is to override the OnRetainNonConfigurationInstance method. Here's a quick sample of how to do it.
First of all, we need a class, that can handle all of our stuff.

Here is a wrapper for all the things we need to save:

public class MainListAdapterWrapper : Java.Lang.Object
{
    public Android.Widget.IListAdapter Adapter { get; set; }
    public int Position { get; set; }
    public List<YourObject> Items { get; set; }
}

在我们的活动中,我们需要保存变量,以存储所有数据:

In our activity, we need to hold variables, to store all the data:

ListView _listView; //Our ListView
List<YourObject> _yourObjectList; //Our items collection
MainListAdapterWrapper _listBackup; //The instance of the saving state wrapper
MainListAdapter _mListAdapter; //Adapter itself

然后,我们重写活动中的 OnRetainNonConfigurationInstance 方法:

Then, we overriding the OnRetainNonConfigurationInstance method in the activity:

public override Java.Lang.Object OnRetainNonConfigurationInstance()
{
    base.OnRetainNonConfigurationInstance();
    var adapterWrapper = new MainListAdapterWrapper();
    adapterWrapper.Position = this._mListAdapter.CurrentPosition; //I'll explain later from where this came from
    adapterWrapper.Adapter = this._listView.Adapter;
    adapterWrapper.Items = this._yourObjectList;
    return adapterWrapper;
}

最后一步是在OnCreate方法中加载保存的状态:

And the final stage is to load saved state in OnCreate method:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.list);

    this._listView = FindViewById<ListView>(Resource.Id.listView);

    if (LastNonConfigurationInstance != null)
    {
        this._listBackup = LastNonConfigurationInstance as MainListAdapterWrapper;
        this._yourObjectList = this._listBackup.Items;
        this._mListAdapter = this._listBackup.Adapter as MainListAdapter;
        this._listView.Adapter = this._mListAdapter;

        //Scrolling to the last position
        if(this._listBackup.Position > 0)
            this._listView.SetSelection(this._listBackup.Position);
    }
    else
    {
        this._listBackup = new MainListAdapterWrapper();
        //Here is the regular loading routine
    }

}

关于 this._mListAdapter.CurrentPosition... 在我的 MainListAdapter 中,我添加了这个属性:

And about the this._mListAdapter.CurrentPosition... In my MainListAdapter, I added this property:

public int CurrentPosition { get; set; }

而且,在`GetView' 方法中,我做到了:

And the, in the `GetView' method, I did that:

this.CurrentPosition = position - 2;

<小时>

附注

您不必完全按照我在此处展示的方式实施.在这段代码中,我持有很多变量,并在 OnCreate 方法中创建所有例程 - 这是错误的.我这样做,只是为了展示它是如何实现的.


P.S.

You don't have to implement exactly as I showed here. In this code, I'm holding a lot of variables, and making all the routine inside the OnCreate method - that is wrong. I did that, just to show how it can be implemented.

这篇关于在 xamarin、monodroid 中旋转后防止重新加载活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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