在xamarin,monodroid中旋转后防止活动重新加载

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

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

这很好,直到我将“Target API”更改为14.如果我将其更改回12,那么一切正常,但在14,活动正在重新启动(OnCreate方法在轮换后触发)。 那么……你会问为什么我需要“Target API”14? – 简单! 因为在我的应用程序中,我正在播放video,为此我需要“真正的全屏”。 所有API低于14添加“设置”(三个点)按钮。 在HTC的情况下,它是一个大而丑陋的按钮,我无法摆脱。

如果你知道如何做到这两个中的一个(去掉API 12中的“设置”按钮,或者在API 14中改变方向后防止重新加载活动),我将非常感谢你的帮助。

好的……最后我解决了! :)保存活动状态而不是阻止重载活动,从一见钟情看起来有点棘手,但实际上非常简单,这是这种情况的最佳解决方案。 就我而言,我有一个ListView,它从互联网上填充了存储在自定义列表适配器中的项目。 如果更改了设备方向,则重新加载活动,ListView也是如此,我丢失了所有数据。 我需要做的就是覆盖OnRetainNonConfigurationInstance方法。 这是一个如何做的快速示例。
首先,我们需要一个可以处理所有内容的类。

这是我们需要保存的所有东西的包装器:

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

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

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

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

 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方法中加载保存状态:

 protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.list); this._listView = FindViewById(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 ,我添加了这个属性:

 public int CurrentPosition { get; set; } 

并且,在`GetView’方法中,我做到了:

 this.CurrentPosition = position - 2; 

PS

你没有必要像我在这里展示的那样完全实现。 在这段代码中,我持有很多变量,并在OnCreate方法中制作所有例程 – 这是错误的。 我这样做,只是为了展示它是如何实现的。

方向改变时会发生什么 (考虑在手机中启用旋转)?

调用Android重启活动onDestroy() ,然后调用onCreate() ,可以区分onDestroy()调用kill活动或重启app抛出旧答案 。

阻止活动重启

只需将ConfigurationChanges设置为OrientationScreenSize

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

为什么这可能不起作用?

我不认为这不会起作用,但将RetaintInstance设置为true会更多地了解RetainInstance

 class myFragment: Fragment { public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); this.RetainInstance = true; // this to change screen orientation Activity.RequestedOrientation = ScreenOrientation.Landscape; } ..... } 

希望这个帮助

在API 13之上,您需要在ConfigChanges中包含屏幕大小。

如此处所示。

也许将此标记添加到API13 +的活动中会有所帮助吗?

    Interesting Posts