[英]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.