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

查看:63
本文介绍了防止在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)]

这很好,直到我将"Target API"更改为14为止.如果我将其更改回12,则一切正常,但是在14上,活动正在重新启动(旋转后会触发OnCreate方法). 那么...您会问为什么我需要目标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上填充了存储在自定义列表适配器中的项目.如果更改了设备方向,则将重新加载活动,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;


P.S.

您不必完全按照我在此处所示的那样执行.在这段代码中,我拥有很多变量,并使所有例程都包含在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天全站免登陆