[英]How to prevent a project rebuild after modification of XML in MonoDroid?
[英]Prevent activity from reload after rotation in xamarin, monodroid
好的...所以我的问题是防止在方向改变后重新加载活动。 基本上,我做的是这样的:
[Activity(Label = "migs", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
这很好,直到我将“Target API”更改为14.如果我将其更改回12,那么一切正常,但在14,活动正在重新启动(OnCreate方法在轮换后触发)。 那么......你会问为什么我需要“Target API”14? - 简单! 因为在我的应用程序中,我正在播放视频,为此我需要“真正的全屏”。 所有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<YourObject> Items { get; set; }
}
在我们的活动中,我们需要保存变量,以存储所有数据:
ListView _listView; //Our ListView
List<YourObject> _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<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
,我添加了这个属性:
public int CurrentPosition { get; set; }
并且,在`GetView'方法中,我做到了:
this.CurrentPosition = position - 2;
你不必像我在这里展示的那样完全实现。 在这段代码中,我持有很多变量,并在OnCreate
方法中制作所有例程 - 这是错误的。 我这样做,只是为了说明如何实现它。
方向改变时会发生什么 (考虑在手机中启用旋转)?
调用Android重启活动onDestroy()
,然后调用onCreate()
,可以区分onDestroy()
调用kill活动或重启app抛出旧答案 。
阻止活动重启
只需将ConfigurationChanges设置为Orientation
, ScreenSize
[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;
}
.....
}
希望这个帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.