Android的活动生命周期 [英] Android Activity Life Cycle
问题描述
活动1开始活动2点击一个按钮。一旦为活动2静态内容设置,并显示给用户,我想揭开序幕的AsyncTask的。在AsyncTask的的执行的进度应显示给用户。 我的问题是,而不是进度发生活动2的重点是在这之后,活动1仍然可见,而AsyncTask的执行,然后切换到活动后2执行完成。我试过把我的AsyncTask的:
- 的OnCreate
- OnPostCreate
- 的OnStart
- OnResume
... 2活动,但活动2仍然只有一次执行任务的完成变得可见。什么点活动2的生命周期中,我需要把我的AsyncTask,以达到我想要什么? code的情况下你需要它:
活动一开始的AsyncTask的前进之前验证用户的输入。在该任务的OnPostExecute,如果信息是有效的:
意向意图=新的意图(_context的typeof(活性2));
intent.PutExtra(呼叫,_call);
intent.PutExtra(简称网站,_SITE);
intent.PutExtra(服务类型,_serviceType);
intent.PutExtra(优先级,_priority);
_context.StartActivity(意向);
Activity2.cs
公共类活性2:活动
{
私人字符串电话{获得;组; }
私人字符串网站{获得;组; }
私人字符串优先{获得;组; }
私人字符串服务类型{获取;组; }
私人ViewAnimator动画{获得;组; }
私人微调PrioritySpin {获得;组; }
私人微调ProblemSpin {获得;组; }
私人微调CauseSpin {获得;组; }
私人微调RepairSpin {获得;组; }
私人微调LaborHrsSpin {获得;组; }
私人微调LaborDecSpin {获得;组; }
私人微调TravelHrsSpin {获得;组; }
私人微调TravelDecSpin {获得;组; }
私人微调SerlModelSpin {获得;组; }
保护覆盖无效的OnCreate(束捆)
{
base.OnCreate(包);
意向意图=意图;
调用= intent.GetStringExtra(呼叫);
网站= intent.GetStringExtra(简称网站);
优先= intent.GetStringExtra(优先级);
的ServiceType = intent.GetStringExtra(服务类型);
标题=服务报告呼叫#+电话+在现场+站点;
的setContentView(Resource.Layout.Activity2);
动画=(ViewAnimator)FindViewById(Resource.Id.contentContainer);
按键基本=(按钮)FindViewById(Resource.Id.basicBtn);
basic.Click + = WizardClick;
按钮设备=(按钮)FindViewById(Resource.Id.equipmentBtn);
equipment.Click + = WizardClick;
按键部分=(按钮)FindViewById(Resource.Id.partsBtn);
parts.Click + = WizardClick;
按钮评论=(按钮)FindViewById(Resource.Id.commentsBtn);
comments.Click + = WizardClick;
按钮复习=(按钮)FindViewById(Resource.Id.reviewSubmit);
review.Click + = WizardClick;
PrioritySpin =(微调)FindViewById(Resource.Id.prioritySpinner);
ProblemSpin =(微调)FindViewById(Resource.Id.problemSpinner);
CauseSpin =(微调)FindViewById(Resource.Id.causeSpinner);
RepairSpin =(微调)FindViewById(Resource.Id.repairSpinner);
LaborHrsSpin =(微调)FindViewById(Resource.Id.laborHrsSpinner);
LaborDecSpin =(微调)FindViewById(Resource.Id.laborDecSpinner);
TravelHrsSpin =(微调)FindViewById(Resource.Id.travelHrsSpinner);
TravelDecSpin =(微调)FindViewById(Resource.Id.travelDecSpinner);
ArrayAdapter<字符串> priorityAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,优先级());
ArrayAdapter<字符串> problemAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,问题());
ArrayAdapter<字符串> causeAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,因为());
ArrayAdapter<字符串> repairAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,维修());
ArrayAdapter<字符串> hoursAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,时间());
ArrayAdapter<字符串> decAdapter =新的ArrayAdapter<字符串>(这一点,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(优先级));
如果(服务类型==PM)
{
ProblemSpin.SetSelection(Array.IndexOf(问题(),计划));
CauseSpin.SetSelection(Array.IndexOf(原因(),计划));
}
Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
}
保护覆盖无效OnResume()
{
base.OnResume();
SerlModelSpin =(微调)FindViewById(Resource.Id.equipSpinner);
IEquipment equipInterface =新EquipmentHelper(此,动画,5,0);
字符串[] equipList = equipInterface.GetEquipmentList(网站);
ArrayAdapter<字符串> equipAdapter =新的ArrayAdapter<字符串>(这一点,Android.Resource.Layout.SimpleSpinnerDropDownItem,equipList);
SerlModelSpin.Adapter = equipAdapter;
}
}
为什么很重要的活动,两个人在后台,而这AsyncTask的是在后台运行?用户会考虑要显示一个对话框......你为什么不把国旗在AsyncTask的,并等待设置标志并显示该对话框,直到 onResume()
方法执行结束。或者你可以尝试从活动之一,当活动调用静态start方法。或许,通过调用完成(),然后覆盖的onDestroy()方法。它很难得到的时机。
Activity 1 starts Activity 2 with a button click. Once the static content for Activity 2 is set and displayed to the user, I want to kick off an AsyncTask. In the execution of the AsyncTask a ProgressBar should display to the user. My problem is that instead of the ProgressBar occurring after Activity 2 is focused, Activity 1 remains visible while the AsyncTask executes and then switches over to Activity 2 after execution is complete. I've tried placing my AsyncTask in:
- OnCreate
- OnPostCreate
- OnStart
- OnResume
...of Activity 2, but Activity 2 still only becomes visible once the task execution completes. What point in Activity 2's life cycle do I need to place my AsyncTask in order to achieve what I want? Code in case you need it:
Activity 1 starts an AsyncTask to validate the user's input before moving forward. In OnPostExecute of that task, if the information is valid:
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;
}
}
Why is it important that activity two be in the background while this AsyncTask is running in the background? The user is going to be shown a dialog regardless... Why don't you put a flag in the AsyncTask, and wait to set the flag and display the dialog until the onResume()
method has finished executing. Or you could try calling a static start method from activity one when activity. Perhaps by calling finish() and then overriding the onDestroy() method. Its hard to get that timing..
这篇关于Android的活动生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!