Android活动生命周期
问题描述:
活动1通过单击按钮启动活动2。一旦Activity 2的静态内容被设置并显示给用户,我想启动一个AsyncTask。在执行AsyncTask时,ProgressBar应显示给用户。 我的问题是,在活动2关注之后,活动1不会在ProgressBar发生时发生,活动1在AsyncTask执行时保持可见状态,然后在执行完成后切换到活动2。我试图把我的AsyncTask在:Android活动生命周期
- 的OnCreate
- OnPostCreate
- 的OnStart
- 的onResume
...活动2,但活动还是2只变成一次可见任务执行完成。在活动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;
}
}
答
为什么重要的活动,两个人在后台,而这是的AsyncTask在后台运行?不管...为什么不在AsyncTask中放置一个标志,并等待设置标志并显示对话框,直到onResume()
方法完成执行。或者,您可以尝试在活动时从活动一中调用静态启动方法。也许通过调用finish()然后重写onDestroy()方法。它很难得到那个时间。
AsyncTask是怎么样的?请记住在实际的UI线程上更新UI事物。活动生命周期可以在这里看到:https://developer.android.com/reference/android/app/Activity.html – Cheesebaron 2012-07-12 19:32:35
http://*.com/a/8516056/265167 – 2013-01-10 11:22:24