2012-07-11 154 views
0

活動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; 
    } 
} 
+0

AsyncTask是怎麼樣的?請記住在實際的UI線程上更新UI事物。活動生命週期可以在這裏看到:https://developer.android.com/reference/android/app/Activity.html – Cheesebaron 2012-07-12 19:32:35

+0

http://stackoverflow.com/a/8516056/265167 – 2013-01-10 11:22:24

回答

0

爲什麼重要的活動,兩個人在後臺,而這是的AsyncTask在後臺運行?不管...爲什麼不在AsyncTask中放置一個標誌,並等待設置標誌並顯示對話框,直到onResume()方法完成執行。或者,您可以嘗試在活動時從活動一中調用靜態啓動方法。也許通過調用finish()然後重寫onDestroy()方法。它很難得到那個時間。