活动1通过单击按钮启动活动2。一旦设置了活动2的静态内容并向用户显示,我想启动AsyncTask。在执行AsyncTask时,ProgressBar应显示给用户。 我的问题是,在活动2聚焦之后,而不是ProgressBar,而AsyncTask执行时活动1仍然可见,然后在执行完成后切换到活动2。我已经尝试将AsyncTask放入:
...但是,只有在任务执行完成后,活动2才会变得可见。在Activity 2的生命周期中,我需要放置我的AsyncTask以实现我想要的东西?代码,以备不时之需:
活动1启动AsyncTask以在向前移动之前验证用户的输入。在该任务的OnPostExecute中,如果信息有效:
Intent intent = new Intent(_context, typeof(Activity2));
intent.PutExtra("Call", _call);
intent.PutExtra("Site", _site);
intent.PutExtra("ServiceType", _serviceType);
intent.PutExtra("Priority", _priority);
_context.StartActivity(intent);
Activity2.cs
public class Activity2 : Activity
{
private string Call { get; set; }
private string Site { get; set; }
private string Priority { get; set; }
private string ServiceType { get; set; }
private ViewAnimator Animator { get; set; }
private Spinner PrioritySpin { get; set; }
private Spinner ProblemSpin { get; set; }
private Spinner CauseSpin { get; set; }
private Spinner RepairSpin { get; set; }
private Spinner LaborHrsSpin { get; set; }
private Spinner LaborDecSpin { get; set; }
private Spinner TravelHrsSpin { get; set; }
private Spinner TravelDecSpin { get; set; }
private Spinner SerlModelSpin { get; set; }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Intent intent = Intent;
Call = intent.GetStringExtra("Call");
Site = intent.GetStringExtra("Site");
Priority = intent.GetStringExtra("Priority");
ServiceType = intent.GetStringExtra("ServiceType");
Title = "Service Report for Call #" + Call + " at Site " + Site;
SetContentView(Resource.Layout.Activity2);
Animator = (ViewAnimator) FindViewById(Resource.Id.contentContainer);
Button basic = (Button) FindViewById(Resource.Id.basicBtn);
basic.Click += WizardClick;
Button equipment = (Button) FindViewById(Resource.Id.equipmentBtn);
equipment.Click += WizardClick;
Button parts = (Button) FindViewById(Resource.Id.partsBtn);
parts.Click += WizardClick;
Button comments = (Button) FindViewById(Resource.Id.commentsBtn);
comments.Click += WizardClick;
Button review = (Button) FindViewById(Resource.Id.reviewSubmit);
review.Click += WizardClick;
PrioritySpin = (Spinner) FindViewById(Resource.Id.prioritySpinner);
ProblemSpin = (Spinner) FindViewById(Resource.Id.problemSpinner);
CauseSpin = (Spinner) FindViewById(Resource.Id.causeSpinner);
RepairSpin = (Spinner) FindViewById(Resource.Id.repairSpinner);
LaborHrsSpin = (Spinner) FindViewById(Resource.Id.laborHrsSpinner);
LaborDecSpin = (Spinner) FindViewById(Resource.Id.laborDecSpinner);
TravelHrsSpin = (Spinner) FindViewById(Resource.Id.travelHrsSpinner);
TravelDecSpin = (Spinner) FindViewById(Resource.Id.travelDecSpinner);
ArrayAdapter<string> priorityAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Priorities());
ArrayAdapter<string> problemAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Problems());
ArrayAdapter<string> causeAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Cause());
ArrayAdapter<string> repairAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Repair());
ArrayAdapter<string> hoursAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Hours());
ArrayAdapter<string> decAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, QuarterHours());
PrioritySpin.Adapter = priorityAdapter;
ProblemSpin.Adapter = problemAdapter;
CauseSpin.Adapter = causeAdapter;
RepairSpin.Adapter = repairAdapter;
LaborHrsSpin.Adapter = hoursAdapter;
LaborDecSpin.Adapter = decAdapter;
TravelHrsSpin.Adapter = hoursAdapter;
TravelDecSpin.Adapter = decAdapter;
PrioritySpin.SetSelection(Convert.ToInt32(Priority));
if (ServiceType == "PM")
{
ProblemSpin.SetSelection(Array.IndexOf(Problems(), "Scheduled"));
CauseSpin.SetSelection(Array.IndexOf(Cause(), "Scheduled"));
}
Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
}
protected override void OnResume()
{
base.OnResume();
SerlModelSpin = (Spinner)FindViewById(Resource.Id.equipSpinner);
IEquipment equipInterface = new EquipmentHelper(this, Animator, 5, 0);
string[] equipList = equipInterface.GetEquipmentList(Site);
ArrayAdapter<string> equipAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, equipList);
SerlModelSpin.Adapter = equipAdapter;
}
}
答案 0 :(得分:0)
当AsyncTask在后台运行时,为什么活动二在后台很重要?无论如何,用户都将显示一个对话框...为什么不在AsyncTask中放置一个标志,并等待设置标志并显示对话框,直到onResume()
方法执行完毕。或者您可以尝试在活动时从活动1调用静态启动方法。也许通过调用finish()然后重写onDestroy()方法。很难得到那个时机..