Android Activity 生命周期 [英] Android Activity Life Cycle

查看:24
本文介绍了Android Activity 生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
  • 启动
  • 重新开始

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

...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 启动一个 AsyncTask 以在继续之前验证用户的输入.在该任务的 OnPostExecute 中,如果信息有效:

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

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

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 Activity 生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆