0

活动 1 通过单击按钮启动活动 2。一旦 Activity 2 的静态内容设置并显示给用户,我想启动一个 AsyncTask。在执行 AsyncTask 时,应该向用户显示 ProgressBar。我的问题是,不是在 Activity 2 聚焦后出现 ProgressBar,而是在 AsyncTask 执行时 Activity 1 保持可见,然后在执行完成后切换到 Activity 2。我试过把我的 AsyncTask 放在:

  • 创建时
  • OnPostCreate
  • 启动
  • 恢复

...的活动 2,但活动 2 仍然只有在任务执行完成后才可见。为了实现我想要的,我需要在 Activity 2 的生命周期中放置 AsyncTask 的哪个点?代码以备不时之需:

Activity 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;
    }
}
4

1 回答 1

0

为什么在此 AsyncTask 在后台运行时,活动 2 在后台很重要?无论如何,用户都将看到一个对话框......为什么不在 AsyncTask 中放置一个标志,并等待设置标志并显示对话框,直到onResume()方法完成执行。或者您可以尝试在活动时从活动一中调用静态启动方法。也许通过调用 finish() 然后覆盖 onDestroy() 方法。很难把握好时机。。

于 2012-07-12T20:55:46.317 回答