0

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;
    }
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
jmease
  • 2,507
  • 5
  • 49
  • 89
  • How does that AsyncTask look like? Remember to update UI things on the actual UI thread. The Activity lifecycle can be seen here: https://developer.android.com/reference/android/app/Activity.html – Cheesebaron Jul 12 '12 at 19:32
  • http://stackoverflow.com/a/8516056/265167 – Yaqub Ahmad Jan 10 '13 at 11:22

1 Answers1

0

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..

Joel
  • 4,732
  • 9
  • 39
  • 54